Print all key/value pairs in a Java ConcurrentHashMap

前端 未结 6 720
清歌不尽
清歌不尽 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 18:01

      //best and simple way to show keys and values
    
        //initialize map
        Map map = new HashMap();
    
       //Add some values
        map.put(1, "Hi");
        map.put(2, "Hello");
    
        // iterate map using entryset in for loop
        for(Entry entry : map.entrySet())
        {   //print keys and values
             System.out.println(entry.getKey() + " : " +entry.getValue());
        }
    
       //Result : 
        1 : Hi
        2 : Hello
    

提交回复
热议问题