Iterator in Java with Examples

Iterator in java with examples featured image

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:

What is ListIterator, What are all the available methods in ListIterator, How to use ListIterator in Java and Example Programs.

 

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:


// 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());
}

 

Output:


Iterator with ArrayList ::: Google.com
Iterator with ArrayList ::: Couponzcorner.com
Iterator with ArrayList ::: Facebook.com

 

 

Iterating Set Interface[HashSet] using Iterator:


// 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());
}

 

Output:


Iterator with HashSet ::: Youtube.com
Iterator with HashSet ::: Javadomain.in
Iterator with HashSet ::: Couponzcorner.com

 

 

Iterating Map Interface[HashMap] using Iterator – keySet():


// 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");
}

 

Output:


Site: Google.com | Purpose: Search Engine

Site: Facebook.com | Purpose: Social Networking

Site: Javadomain.in | Purpose: Technical Blog

 

 

Iterating Map Interface[HashMap] using Iterator – entrySet():


// 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());
}

 

Output:


Google.com=Search Engine
Facebook.com=Social Networking
Javadomain.in=Technical Blog

 

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:


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

 

Output:


[Google.com, Couponzcorner.com]

604 total views, 2 views today

About: Mirthbees

Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in