The following example demonstrates how to use the iterator() method of the Collection class to traverse the collection:
/* author by w3cschool.cc Main.java */import java.util.*;import java.util.*;public class Main { public static void main(String[] args) { HashMap< String, String> hMap = new HashMap< String, String>(); hMap.put("1", "1st"); hMap.put("2", "2nd"); hMap.put("3", "3rd"); Collection cl = hMap.values(); Iterator itr = cl.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } }}
The output result of running the above code is:
3rd 2nd 1st
The above is the Java example - HashMap Traversed content, please pay attention to the PHP Chinese website (m.sbmmt.com) for more related content!