How to put an Entry into a Map?

后端 未结 4 1134
你的背包
你的背包 2021-01-03 17:36

Is there any way that I can put a whole Entry object to a Map object like:

map.put(entry);

instead of passing a k

4条回答
  •  花落未央
    2021-01-03 18:33

    I have searched on the Map interface methods but there is no method that takes an entry and puts it in the map. Therefore I have implemented it by myself using a little bit of inheritance and Java 8 interfaces.

    import java.util.AbstractMap;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.TreeMap;
    
    public class Maps {
    
        // Test method
        public static void main(String[] args) {
            Map.Entry entry1 = newEntry("Key1", "Value1");
            Map.Entry entry2 = newEntry("Key2", "Value2");
    
            System.out.println("HashMap");
            MyMap hashMap = new MyHashMap<>();
            hashMap.put(entry1);
            hashMap.put(entry2);
    
            for (String key : hashMap.keySet()) {
                System.out.println(key + " = " + hashMap.get(key));
            }
    
            System.out.println("\nTreeMap");
            MyMap treeMap = new MyTreeMap<>();
            treeMap.put(entry1);
            treeMap.put(entry2);
    
    
            for (String key : treeMap.keySet()) {
                System.out.println(key + " = " + treeMap.get(key));
            }
        }
    
    
        /**
         * Creates a new Entry object given a key-value pair.
         * This is just a helper method for concisely creating a new Entry.
         * @param key   key of the entry
         * @param value value of the entry
         * 
         * @return  the Entry object containing the given key-value pair
         */
        private static  Map.Entry newEntry(K key, V value) {
            return new AbstractMap.SimpleEntry<>(key, value);
        }
    
        /**
         * An enhanced Map interface.
         */
        public static interface MyMap extends Map {
    
            /**
             * Puts a whole entry containing a key-value pair to the map.
             * @param entry 
             */
            public default V put(Entry entry) {
                return put(entry.getKey(), entry.getValue());
            }
        }
    
        /**
         * An enhanced HashMap class.
         */
        public static class MyHashMap extends HashMap implements MyMap {}
    
        /**
         * An enhanced TreeMap class.
         */
        public static class MyTreeMap extends TreeMap implements MyMap {}
    }
    

    The MyMap interface is just an interface that extends the Map interface by adding one more method, the public default V put(Entry entry). Apart from just defining the method, a default implementation is coded too. Doing that, we can now add this method to any class that implements the Map interface just by defining a new class that implements the MyMap interface and extending the map implementation class of our choice. All of that in one line! This is demonstrated in the bottom of the code above where two classes are created each extending the HashMap and the TreeMap implementations.

提交回复
热议问题