Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

java - How to put an Entry into a Map?

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

map.put(entry);

instead of passing a key-value pair like:

map.put(key,value);
question from:https://stackoverflow.com/questions/39441096/how-to-put-an-entry-into-a-map

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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<String, String> entry1 = newEntry("Key1", "Value1");
        Map.Entry<String, String> entry2 = newEntry("Key2", "Value2");

        System.out.println("HashMap");
        MyMap<String, String> hashMap = new MyHashMap<>();
        hashMap.put(entry1);
        hashMap.put(entry2);

        for (String key : hashMap.keySet()) {
            System.out.println(key + " = " + hashMap.get(key));
        }

        System.out.println("
TreeMap");
        MyMap<String, String> 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 <K,V> Map.Entry<K,V> newEntry(K key, V value) {
        return new AbstractMap.SimpleEntry<>(key, value);
    }

    /**
     * An enhanced Map interface.
     */
    public static interface MyMap<K,V> extends Map<K,V> {

        /**
         * Puts a whole entry containing a key-value pair to the map.
         * @param entry 
         */
        public default V put(Entry<K,V> entry) {
            return put(entry.getKey(), entry.getValue());
        }
    }

    /**
     * An enhanced HashMap class.
     */
    public static class MyHashMap<K,V> extends HashMap<K,V> implements MyMap<K,V> {}

    /**
     * An enhanced TreeMap class.
     */
    public static class MyTreeMap<K,V> extends TreeMap<K,V> implements MyMap<K,V> {}
}

The MyMap interface is just an interface that extends the Map interface by adding one more method, the public default V put(Entry<K,V> 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...