How to put an Entry into a Map?

后端 未结 4 1131
你的背包
你的背包 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:06

    Solution if you're using Java Platform SE 8:

    SimpleEntry and SimpleImmutableEntry classes implement interface Map.Entry

    import java.util.AbstractMap.SimpleEntry;
    import java.util.AbstractMap.SimpleImmutableEntry;
    
    List> map = new ArrayList<>(); 
    map.add(new SimpleEntry(key,value));
    map.add(new SimpleImmutableEntry(key,value)); // constructs immutable entries for thread-safe operation
    

提交回复
热议问题