Site icon NgDeveloper

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:

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:

 

Available methods in Iterator:

 

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]

Exit mobile version