Table of Contents
Iterator:
Iterator is an object in general to enable the traverse to any collection. Iterator can be used in java to iterate List/Set/Map interfaces or List/Set/Map type of objects.
[eg: ArrayList/LinkedList/HashSet/LinkedHashSet/TreeSet/LinkedHashMap/HashMap etc..]
Iterator has below limitations:
- Iterator can be used to traverse only in forward direction.
- No set() method:so we can not set a value of the collection using iterator.
- No previousIndex()/nextIndex() method: Getting next index value/previous index value is not possible with iterator.
- No add() method: we can not add anything using iterator to our collection objects, where as ListIterator provides add() method to add the elements to lists.
Note: All these limitations can be resolved with ListIterator.
Read here:
Remember:
- Traversing in both forward and backward/reverse direction is possible with ListIterator, but ListIterator supports only List interface. So Set/Map with ListIterator involves an extra step to convert to list and proceed.
- Also ListIterator has set() method which helps to set the value of the collection whichever is returned by next()/previous() elements.
Available methods in Iterator:
- hasNext()
- next()
- remove()
hasNext()/next():
Iterating List Interface[ArrayList] using Iterator:
[java]
// Iterating ArrayList/List interface using Iterator
List myList = new LinkedList();
myList.add("Google.com");
myList.add("Couponzcorner.com");
myList.add("Facebook.com");
Iterator mlIter = myList.iterator();
while(mlIter.hasNext()){
System.out.println("Iterator with ArrayList ::: "+mlIter.next());
}
[/java]
Output:
[plain]
Iterator with ArrayList ::: Google.com
Iterator with ArrayList ::: Couponzcorner.com
Iterator with ArrayList ::: Facebook.com
[/plain]
Iterating Set Interface[HashSet] using Iterator:
[java]
// Iterating HashSet/Set Interface using Iterator
Set mySet = new HashSet();
mySet.add("Javadomain.in");
mySet.add("Couponzcorner.com");
mySet.add("Youtube.com");
Iterator msIter = mySet.iterator();
while(msIter.hasNext()){
System.out.println("Iterator with HashSet ::: "+msIter.next());
}
[/java]
Output:
[plain]
Iterator with HashSet ::: Youtube.com
Iterator with HashSet ::: Javadomain.in
Iterator with HashSet ::: Couponzcorner.com
[/plain]
Iterating Map Interface[HashMap] using Iterator – keySet():
[java]
// Iterating HashMap/Map Interface using Iterator
Map<String,String> myMap = new HashMap<String,String>();
myMap.put("Google.com", "Search Engine");
myMap.put("Facebook.com", "Social Networking");
myMap.put("Javadomain.in", "Technical Blog");
Iterator mmIter = myMap.keySet().iterator();
while(mmIter.hasNext()){
String siteNm = (String) mmIter.next();
System.out.println("Site: "+siteNm+" | Purpose: "+myMap.get(siteNm)+"\n");
}
[/java]
Output:
[plain]
Site: Google.com | Purpose: Search Engine
Site: Facebook.com | Purpose: Social Networking
Site: Javadomain.in | Purpose: Technical Blog
[/plain]
Iterating Map Interface[HashMap] using Iterator – entrySet():
[java]
// Iterating HashMap/Map Interface using Iterator
Map<String,String> myMap = new HashMap<String,String>();
myMap.put("Google.com", "Search Engine");
myMap.put("Facebook.com", "Social Networking");
myMap.put("Javadomain.in", "Technical Blog");
Iterator mmIter = myMap.entrySet().iterator();
while(mmIter.hasNext()){
System.out.println(mmIter.next());
}
[/java]
Output:
[plain]
Google.com=Search Engine
Facebook.com=Social Networking
Javadomain.in=Technical Blog
[/plain]
Remember:
We have used keySet() to iterate map using iterator because We can not iterate the map directly like how we are doing directly with Set/List interface. So convert the map to Set using keySet()/entrySet() methods before using iterator to iterate the map.
keySet() : returns all the map keys alone.
entrySet(): returns both keys and values together.
remove():
Iterator remove() method Example:
[java]
List myList = new LinkedList();
myList.add("Google.com");
myList.add("Couponzcorner.com");
myList.add("Facebook.com");
Iterator mlIter = myList.iterator();
while(mlIter.hasNext()){
if(mlIter.next().toString().equalsIgnoreCase("Facebook.com")){
mlIter.remove();
}
}
System.out.println(myList);
[/java]
Output:
[plain]
[Google.com, Couponzcorner.com]
[/plain]