如何将条目放入地图

如何将条目放入地图

本文介绍了如何将条目放入地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以将整个Entry对象放置到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);

推荐答案

我已经在Map接口上搜索了方法,但是没有方法可以接受一个条目并将其放入地图中.因此,我已经实现了我自己使用一点继承和Java 8接口.

I have searched on the Map interface methods but there is no method thattakes an entry and puts it in the map. Therefore I have implemented itby 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("\nTreeMap");
        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> {}
}

MyMap接口只是扩展Map接口的接口通过添加另一种方法public default V put(Entry<K,V> entry).除了定义方法外,还对默认实现进行了编码也.这样做,我们现在可以将此方法添加到任何实现的类中Map接口,只需定义一个新类即可实现MyMap接口并扩展了我们选择的地图实现类.全部在一行中!这在上面代码的底部展示了两个创建每个扩展HashMap和TreeMap的类实施.

The MyMap interface is just an interface that extends the Map interfaceby adding one more method, the public default V put(Entry<K,V> entry).Apart from just defining the method, a default implementation is codedtoo. Doing that, we can now add this method to any class that implementsthe Map interface just by defining a new class that implements theMyMap interface and extending the map implementation class of our choice. Allof that in one line! This is demonstrated in the bottom of the code above where twoclasses are created each extending the HashMap and the TreeMapimplementations.

这篇关于如何将条目放入地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:07