YouTip LogoYouTip

Collection Iterator

[![Image 1: Java Examples](#) Java Examples](#) The following examples demonstrate how to iterate over List, Set, and Map collections that extend from the Collection interface. We use a regular for loop, an enhanced for loop, and an iterator, among other methods, to traverse the collections: ### Iterating over List and Set Type Collections ## Main.java File import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; public class Main{public static void main(String[]args){listTest(); setTest(); }private static void setTest(){Setset = new HashSet(); set.add("JAVA"); set.add("C"); set.add("C++"); set.add("JAVA"); set.add("JAVASCRIPT"); Iteratorit = set.iterator(); while(it.hasNext()){String value = it.next(); System.out.println(value); }for(String s: set){System.out.println(s); }}private static void listTest(){Listlist = new ArrayList(); list.add("Rookie"); list.add("Bird"); list.add("Teach"); list.add("Program"); list.add("www..com"); Iteratorit = list.iterator(); while(it.hasNext()){String value = it.next(); System.out.println(value); }for(int i = 0, size = list.size(); i<size; i++){String value = list.get(i); System.out.println(value); }for(String value : list){System.out.println(value); }}} The output of the above code is: www..com www..com www..com JAVA JAVASCRIPT C++ C JAVA JAVASCRIPT C++ C ### Iterating over Map Type Collections In the following example, we use the `keySet()` and `entrySet()` methods of HashMap to iterate over the collection: /* author by .com Main.java */import java.util.Map;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;import java.util.Map.Entry;//Enhanced For Looppublic class Main { public static void main(String[] args) { // Create a HashMap object and add some key-value pairs. Map maps = new HashMap(); maps.put("1", "PHP"); maps.put("2", "Java"); maps.put("3", "C"); maps.put("4", "C++"); maps.put("5", "HTML"); // Traditional method 1 to iterate over a map collection; keySet() //traditionalMethod1(maps); // Traditional method 2 to iterate over a map collection; entrySet() //traditionalMethod2(maps); // Method 1 to iterate over a map collection using an enhanced for loop; keySet() //strongForMethod1(maps); // Method 2 to iterate over a map collection using an enhanced for loop; entrySet() strongForMethod2(maps); } private static void strongForMethod2(Map maps) { Set<Entry> set = maps.entrySet(); for (Entry entry : set) { String key = entry.getKey(); String value = entry.getValue(); System.out.println(key + " : " + value); } } private static void strongForMethod1(Map maps) { Set set = maps.keySet(); for (String s : set) { String key = s; String value = maps.get(s); System.out.println(key + " : " + value); } } // Use the entrySet() method to get each key-value pair in the maps collection. private static void traditionalMethod2(Map maps) { Set<Map.Entry> sets = maps.entrySet(); // Get an iterator to traverse the corresponding values. Iterator<Entry> it = sets.iterator(); while (it.hasNext()) { Map.Entry entry = (Entry) it.next(); String key = entry.getKey(); String value = entry.getValue(); System.out.println(key + " : " + value); } } // Use the keySet() method to get all keys in the maps collection, then traverse the keys to get the corresponding values. private static void traditionalMethod1(Map maps) { Set sets = maps.keySet(); // Get an iterator to traverse the corresponding values Iterator it = sets.iterator(); while (it.hasNext()) { String key = it.next(); String value = maps.get(key); System.out.println(key + " : " + value); } }} The output of the above code is: 1 : PHP 2 : Java3 : C 4 : C++5 : HTML [![Image 2: Java Examples](#) Java Examples](#)
← Collection SizeCollection Remove β†’