@Override
public void intersect(Set<E> set) {
Iterator<E> iteratorSet = set.iterator();
Iterator<E> thisIterator = this.iterator();
SetNode<E> head = new SetNode<>(null,null);
SetNode<E> curNode = head;
Optional<E> optionalSet = getOptional(iteratorSet);
Optional<E> optionalThis = getOptional(thisIterator);
while (optionalSet.isPresent() && optionalThis.isPresent()) {
SetNode<E> nextNode;
int compare = optionalThis.get().compareTo(optionalSet.get());
if(compare == 0) {
nextNode = new SetNode<>(optionalThis.get());
curNode.next = nextNode;
curNode = nextNode;
optionalThis = getOptional(thisIterator);
optionalSet = getOptional(iteratorSet);
}
else if(compare < 0)
optionalThis = getOptional(thisIterator);
else
optionalSet = getOptional(iteratorSet);
}
this.head = head.next;
}
In my university I was supposed to create a ListSet class that has a intersect function, that takes in the intersect of two set that are sorted. I don't understand why we need to do
SetNode<E> head = new SetNode<>(null,null);
SetNode<E> curNode = head;
I'm guessing you were supposed to create a DoublyLinkedList with Set functionality for your task. Here is a visualization of it: http://bridgesuncc.github.io/tutorials/DoublyLinkedList.html
Linked List is storing a reference to the first(head) element of the list instead of storing all the references in an array(the way ArrayList does it). I'm guessing this
headelement is used as a constructor parameter later in the code, something like:While
currNodeis used to store reference to the current element and get's updated in the cycle.