Vector
Stack
Hashtable
Bitset
β¦.<aside> π‘ Create a hierarchy of abstract interfaces and concrete implementations β Collection
</aside>
public interface Collection<E>{
boolean add(E element);
Iterator<E> iterator();
...
}
add()
β Add to collection
iterator()
β Get an object that implements Iterator interface.
public interface Iterator<E>{
E next();
boolean hasNext();
void remove();
...
}
With this iterator: you can loop through the elements.
Collection<String> cstr = new .... ;
Iterator<String> iter = cstr.iterator();
while(iter.hasNext()){
String element = iter.next();
// Do something with element.
}
<aside> π‘ Java later added for each loop β Implicitly creates an iterator and runs through it.
</aside>
Collection<String> cstr = new .... ;
for(String element : cstr){
// do something with element
}
Can use generic functions to operate on collections due to its hierarchy.
public static <E> boolean contains(Collection<E> c, Object obj){
for(E element : c){
if(element.equals(obj)){
return true;
}
}
return false;
}
element.equals(obj)
works??? β AnswerRecall the interface Iterator:
public interface Iterator<E>{
E next();
boolean hasNext();
void remove();
...
}
Note the remove()
method β which element does it remove ( first element, last element, any other elementβ¦.????)