From charlesreid1

m (Replacing charlesreid1.com:3000 with git.charlesreid1.com)
 
(2 intermediate revisions by the same user not shown)
Line 13: Line 13:
If you use the built-in container types, you also see generic class types. It is the concept that the container can deal with objects of any type, because the container just needs to hold them, or set and get container elements, but never interact with the elements themselves. That's the reason behind the <code>LinkedList<String></code> syntax.
If you use the built-in container types, you also see generic class types. It is the concept that the container can deal with objects of any type, because the container just needs to hold them, or set and get container elements, but never interact with the elements themselves. That's the reason behind the <code>LinkedList<String></code> syntax.


The important part here is the diamond notation <code><E></code> - the letter that comes after the diamond is arbitrary but it is the keyword indicating the generic type of object that this Node class wraps. Later in the class, the object is referred to using a pointer of generic type E.
The important part here is the diamond notation <code><String></code> - when we define a generic class we deal with a passed parameter of type <code><E></code>, where the letter that goes in the diamond is arbitrary but is the keyword indicating the generic type of object that this Node class wraps. Later in the class, the object is referred to using a pointer of generic type E.
 
[[Maps]] and [[Dictionaries]] have keys and values that can each take on different types, but Java implements a similar idea for maps by using a similar idea, defining a <code>Map<K,V></code> that allows the class definition to deal with a generic key/value type.


===Example===
===Example===
Line 19: Line 21:
This is an example of a Node class found in a linked binary tree class (see [[Binary Trees/LinkedBinTree]] for actual implementation):
This is an example of a Node class found in a linked binary tree class (see [[Binary Trees/LinkedBinTree]] for actual implementation):


git.charlesreid1.com link: https://charlesreid1.com:3000/cs/java/src/master/trees/oop-correct-tree/LinkedBinTree.java
git.charlesreid1.com link: https://git.charlesreid1.com/cs/java/src/master/trees/oop-correct-tree/LinkedBinTree.java


<pre>
<pre>
Line 27: Line 29:
// No need to expose things.
// No need to expose things.
private E element;
private E element;
private Node<E> parent; // pointer to parent node
private Node<E> parent; // pointer to parent node
private Node<E> left;  // pointer to left child
private Node<E> left;  // pointer to left child
private Node<E> right; // pointer to right child
private Node<E> right; // pointer to right child
// constructor with element and neighbors
// constructor with element and neighbors
public Node(E e, Node<E> above, Node<E> lefty, Node<E> righty) {  
public Node(E e, Node<E> above, Node<E> lefty, Node<E> righty) {  
Line 37: Line 41:
right = righty;
right = righty;
}
}
// get methods - one for each attribute
// get methods - one for each attribute
public E getElement() { return element; }
public E getElement() { return element; }
public Node<E> getParent() { return parent; }
public Node<E> getParent() { return parent; }
public Node<E> getLeft() { return left; }
public Node<E> getLeft() { return left; }
public Node<E> getRight() { return right; }
public Node<E> getRight() { return right; }
// update methods
// update methods
public void setElement(E e) { element = e; }
public void setElement(E e) { element = e; }
public void setParent(Node<E> up) { parent = up; }
public void setParent(Node<E> up) { parent = up; }
public void setLeft(Node<E> lefty) { left = lefty; }
public void setLeft(Node<E> lefty) { left = lefty; }

Latest revision as of 03:31, 9 October 2019

Can use Java Generics to define classes that wrap generic types.

Can also define methods that take generic types of arguments, slightly different syntax.

Let's look at each.

Generic Classes

Applications

One common application of generic classes is when building data structures like linked lists or trees. These objects use node structures to store data under the hood, and these node structures often take the form of objects storing an element of data of arbitrary type, along with pointers.

If you use the built-in container types, you also see generic class types. It is the concept that the container can deal with objects of any type, because the container just needs to hold them, or set and get container elements, but never interact with the elements themselves. That's the reason behind the LinkedList<String> syntax.

The important part here is the diamond notation <String> - when we define a generic class we deal with a passed parameter of type <E>, where the letter that goes in the diamond is arbitrary but is the keyword indicating the generic type of object that this Node class wraps. Later in the class, the object is referred to using a pointer of generic type E.

Maps and Dictionaries have keys and values that can each take on different types, but Java implements a similar idea for maps by using a similar idea, defining a Map<K,V> that allows the class definition to deal with a generic key/value type.

Example

This is an example of a Node class found in a linked binary tree class (see Binary Trees/LinkedBinTree for actual implementation):

git.charlesreid1.com link: https://git.charlesreid1.com/cs/java/src/master/trees/oop-correct-tree/LinkedBinTree.java

public class LinkedBinaryTree<E> {

	protected static class Node<E> {
		// No need to expose things.
		private E element;

		private Node<E> parent; // pointer to parent node
		private Node<E> left;  // pointer to left child
		private Node<E> right; // pointer to right child

		// constructor with element and neighbors
		public Node(E e, Node<E> above, Node<E> lefty, Node<E> righty) { 
			element = e;
			parent = above;
			left = lefty;
			right = righty;
		}

		// get methods - one for each attribute
		public E getElement() { return element; }

		public Node<E> getParent() { return parent; }
		public Node<E> getLeft() { return left; }
		public Node<E> getRight() { return right; }

		// update methods
		public void setElement(E e) { element = e; }

		public void setParent(Node<E> up) { parent = up; }
		public void setLeft(Node<E> lefty) { left = lefty; }
		public void setRight(Node<E> righty) { right = righty; }

	}