Print all key/value pairs in a Java ConcurrentHashMap

前端 未结 6 723
清歌不尽
清歌不尽 2021-02-02 17:40

I am trying to simply print all key/value pair(s) in a ConcurrentHashMap.

I found this code online that I thought would do it, but it seems to be getting information ab

6条回答
  •  情书的邮戳
    2021-02-02 17:41

    I tested your code and works properly. I've added a small demo with another way to print all the data in the map:

    ConcurrentHashMap map = new ConcurrentHashMap();
    map.put("A", 1);
    map.put("B", 2);
    map.put("C", 3);
    
    for (String key : map.keySet()) {
        System.out.println(key + " " + map.get(key));
    }
    
    for (Map.Entry entry : map.entrySet()) {
        String key = entry.getKey().toString();
        Integer value = entry.getValue();
        System.out.println("key, " + key + " value " + value);
    }
    

提交回复
热议问题