Java - CollectionFramework - Examples A
Java - CollectionFramework - Examples A
Framework: Examples
Using Set
4
Map views
• Set<Map.Entry<K, V>> entrySet()
Returns a set view of the mappings contained in
this map.
• A view is dynamic access into the Map
If you change the Map, the view changes
If you change the view, the Map changes
• The Map interface does not provide any Iterators
However, there are iterators for the above Sets
and Collections
5
import java.util.HashMap;
import java.util.Set;
//Creating a Set
Set set = map.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next(); //a collection-view of the map
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue()); }
if(v.contains(new Integer(3)))
System.out.println("Vector contains 3.");}
Using ListIterator
. you can also obtain an iterator by
For collections that implement List,
calling ListIterator which can traverse the list in either direction
ArrayList al = new ArrayList();