How to put an Entry into a Map?

后端 未结 4 1159
你的背包
你的背包 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条回答
  •  旧时难觅i
    2021-01-03 18:12

    To instantiate a Map with Entries (in the same way as you can do Arrays.asList(T... a) or Sets.newHashSet(T... a) in Google Guava library, I found this:

    import java.util.AbstractMap;
    import java.util.Map;
    
    public class MapWithEntries {
    
        private static final Map.Entry ENTRY_1 = new AbstractMap.SimpleEntry<>("A", "Hello");
        private static final Map.Entry ENTRY_2 = new AbstractMap.SimpleEntry<>("B", "World");
    
        private static final Map MAP_WITH_ENTRIES = Map.ofEntries(ENTRY_1, ENTRY_2);
    }
    

提交回复
热议问题