Object
)In other words, a function or type can be written without depending on knowing the specific types.
<>
after your class name with whatever name you want, convention is to use Type
or T
Now, when calling our class we need to specify the actual type inside the <>
Add <Something>
before return type, use the generic name for the argument type
public static <T> String getMyString(T element) {
return element.getClass().getName() + " = " + element;
}
Call:
Recall what linked lists are? Discuss with your table mates:
To create a linked list:
Node
Write a class in Java called Node
that has two instance variables: data (any type), and a reference to another node.
Write the constructor method, and getters
Node
solutionLinkedList
classMyList
classinsert
method should check if the head/root is null (what’s the easiest implementation?)toString()
method to test your code (get each element in list)MyList
classpublic class MyLinkedList<T> {
private Node<T> root;
public MyLinkedList() {
root = null;
}
public Node<T> getRoot() {
return root;
}
public void insert(Node<T> newNode) {
if (root == null) {
root = newNode;
} else {
newNode.setNext(root);
root = newNode;
}
}
public String toString() {
String message = "";
Node<T> currentNode = root;
while (currentNode != null) {
message += currentNode.getData() + " ";
currentNode = currentNode.getNext();
}
return message.trim();
}
}
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class MyLinkedListTest {
@Test
void testNodeNull() {
Node<Integer> myNode = new Node<>(10);
Assertions.assertNull(myNode.getNext());
}
@Test
void testNodeValue() {
Node<Integer> myNode = new Node<>(10);
Assertions.assertEquals(10, myNode.getData());
}
@Test
void testListNull() {
MyLinkedList<String> myList = new MyLinkedList<>();
Assertions.assertNull(myList.getRoot());
}
@Test
void testListInsert() {
MyLinkedList<Object> myList = new MyLinkedList<>();
myList.insert(new Node<>(10));
Assertions.assertEquals(10, myList.getRoot().getData());
}
@Test
void testListToString() {
MyLinkedList<Object> myList = new MyLinkedList<>();
myList.insert(new Node<>(10));
myList.insert(new Node<>(5));
myList.insert(new Node<>(8));
String expected = "8 5 10";
Assertions.assertEquals(expected, myList.toString());
}
}
search
methodtoString()
false
if value not in list, true
otherwisesearch
solutionRemoving a node takes a few more steps:
delete
methodpublic void delete(T value) {
if (root.getData().equals(value)) {
root = root.getNext();
} else {
Node<T> currentNode = root;
Node<T> previousNode = null;
Boolean found = false;
while (currentNode != null && !found) {
if (currentNode.getData().equals(value) ) {
found = true;
if (previousNode != null) {
previousNode.setNext(currentNode.getNext());
}
} // if found node to delete
previousNode = currentNode; // for next iteration make current previous
currentNode = currentNode.getNext(); // get next node for current
} // while
}
}
@Test
void testDeletion() {
MyLinkedList<Object> myList = new MyLinkedList<>();
myList.insert(new Node<>(10));
myList.insert(new Node<>(5.5));
myList.insert(new Node<>("abc"));
myList.insert(new Node<>("hello"));
myList.delete(5.5);
String expected = "hello abc 10";
Assertions.assertEquals(expected, myList.toString());
}