Home > Java > javaTutorial > Java example - HashMap traversal

Java example - HashMap traversal

黄舟
Release: 2017-01-21 11:01:16
Original
1592 people have browsed it

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());
     }
   }}
Copy after login

The output result of running the above code is:

3rd
2nd
1st
Copy after login

The above is the Java example - HashMap Traversed content, please pay attention to the PHP Chinese website (m.sbmmt.com) for more related content!



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template