zoukankan      html  css  js  c++  java
  • Map 遍历的几种方法

    复习map的过程中想到的,做个简单的记录

     1 public class HashMapTest {
     2 
     3     public static void main(String args[]) {
     4         Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
     5         hm.put(1, 8);
     6         hm.put(2, 7);
     7         hm.put(3, 6);
     8         hm.put(4, 5);
     9         System.out.println(hm);
    10         System.out.println("第一种:foreach循环");
    11         for (Integer i : hm.keySet()) {
    12             Integer a = hm.get(i);
    13             System.out.println(a);
    14         }
    15 
    16         System.out.println("第二种:迭代器");
    17         Iterator<Map.Entry<Integer, Integer>> it = hm.entrySet().iterator();
    18         while (it.hasNext()) {
    19             System.out.println(it.next().getValue());
    20         }
    21 
    22         System.out.println("第三种:");
    23         for (Map.Entry<Integer, Integer> entry : hm.entrySet()) {
    24             System.out.println(entry.getKey() + "--" + entry.getValue());
    25         }
    26 
    27     }
    28 
    29 }
  • 相关阅读:
    ReentrantLock和AQS
    CAS
    java8中ConcurrentHashMap
    java8中的HashMap
    TCP和UDP
    慢查询日志和profiling
    explain的使用
    select、poll、epoll之间的区别
    I/O模型
    生产者-消费者模式
  • 原文地址:https://www.cnblogs.com/wowind/p/7250819.html
Copyright © 2011-2022 走看看