Home  >  Article  >  Java  >  Java example - HashMap traversal

Java example - HashMap traversal

黄舟
黄舟Original
2017-01-21 11:01:161436browse

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 (www.php.cn) for more related content!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn