@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;