Print all key/value pairs in a Java ConcurrentHashMap

前端 未结 6 719
清歌不尽
清歌不尽 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<String, Integer> map = new ConcurrentHashMap<String, Integer>();
    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<String, Integer> entry : map.entrySet()) {
        String key = entry.getKey().toString();
        Integer value = entry.getValue();
        System.out.println("key, " + key + " value " + value);
    }
    
    0 讨论(0)
  • 2021-02-02 17:47

    The ConcurrentHashMap is very similar to the HashMap class, except that ConcurrentHashMap offers internally maintained concurrency. It means you do not need to have synchronized blocks when accessing ConcurrentHashMap in multithreaded application.

    To get all key-value pairs in ConcurrentHashMap, below code which is similar to your code works perfectly:

    //Initialize ConcurrentHashMap instance
    ConcurrentHashMap<String, Integer> m = new ConcurrentHashMap<String, Integer>();
    
    //Print all values stored in ConcurrentHashMap instance
    for each (Entry<String, Integer> e : m.entrySet()) {
      System.out.println(e.getKey()+"="+e.getValue());
    }
    

    Above code is reasonably valid in multi-threaded environment in your application. The reason, I am saying 'reasonably valid' is that, above code yet provides thread safety, still it can decrease the performance of application.

    Hope this helps you.

    0 讨论(0)
  • 2021-02-02 17:51

    Work 100% sure try this code for the get all hashmap key and value

    static HashMap<String, String> map = new HashMap<>();
    
    map.put("one"  " a " );
    map.put("two"  " b " );
    map.put("three"  " c " );
    map.put("four"  " d " );
    

    just call this method whenever you want to show the HashMap value

     private void ShowHashMapValue() {
    
            /**
             * get the Set Of keys from HashMap
             */
            Set setOfKeys = map.keySet();
    
    /**
     * get the Iterator instance from Set
     */
            Iterator iterator = setOfKeys.iterator();
    
    /**
     * Loop the iterator until we reach the last element of the HashMap
     */
            while (iterator.hasNext()) {
    /**
     * next() method returns the next key from Iterator instance.
     * return type of next() method is Object so we need to do DownCasting to String
     */
                String key = (String) iterator.next();
    
    /**
     * once we know the 'key', we can get the value from the HashMap
     * by calling get() method
     */
                String value = map.get(key);
    
                System.out.println("Key: " + key + ", Value: " + value);
            }
        } 
    
    0 讨论(0)
  • 2021-02-02 17:52

    You can do something like

    Iterator iterator = map.keySet().iterator();
    
    while (iterator.hasNext()) {
       String key = iterator.next().toString();
       Integer value = map.get(key);
    
       System.out.println(key + " " + value);
    }
    

    Here 'map' is your concurrent HashMap.

    0 讨论(0)
  • 2021-02-02 17:56

    The HashMap has forEach as part of its structure. You can use that with a lambda expression to print out the contents in a one liner such as:

    map.forEach((k,v)-> System.out.println(k+", "+v));
    
    or
    
    map.forEach((k,v)-> System.out.println("key: "+k+", value: "+v));
    
    0 讨论(0)
  • 2021-02-02 18:01
      //best and simple way to show keys and values
    
        //initialize map
        Map<Integer, String> map = new HashMap<Integer, String>();
    
       //Add some values
        map.put(1, "Hi");
        map.put(2, "Hello");
    
        // iterate map using entryset in for loop
        for(Entry<Integer, String> entry : map.entrySet())
        {   //print keys and values
             System.out.println(entry.getKey() + " : " +entry.getValue());
        }
    
       //Result : 
        1 : Hi
        2 : Hello
    
    0 讨论(0)
提交回复
热议问题