Create a 'custom set' class that has the methods like add(),remove(),contains(),iterator() like a regular set, but we should be able to modify the set while it is being iterated. And the iterator should only contain the elements when it was called.
class CustomSet {
public boolean add(int i){
}
public boolean remove(int i){
}
public boolean contains(int i){
}
public Iterator<Integer> iterator(){
}
public void printAll(){
//print all elements using the iterator
}
}For example calls to the custom set should behave as follows:
CustomSet set = new CustomSet();
set.add(1); //return true
set.add(2); //return true
set.add(3); //return true
set.add(4); //return true
Iterator<Integer> itr1 = set.iterator(); //itrerator should have 1,2,3,4
set.remove(1); //return true;
set.printAll(); //should print 1,2,3,4, because the set had 1,2,3,4 when the iterator was initialized.
set.contains(1); //return false; because 1 was removed.
Iterator<Integer> itr2 = set.iterator(); //itrerator should have 2,3,4
set.printAll(); //should print 2,3,4, because the set had 2,3,4 when the iterator was initialized.