what java collection that provides multiple values for the same key

后端 未结 3 890
清歌不尽
清歌不尽 2021-01-03 00:58

what type of java collection that returns multiple values for the same key?

example, I want to return 301,302,303 for key 300.

3条回答
  •  伪装坚强ぢ
    2021-01-03 01:30

    You could use Multimap, it is under the Apache license.

    See this link. For posterity:

    org.apache.commons.collections
    Interface MultiMap
    
    All Superinterfaces:
        java.util.Map
    
    All Known Implementing Classes:
        MultiHashMap, MultiValueMap
    
    public interface MultiMap
    extends java.util.Map
    
    Defines a map that holds a collection of values against each key.
    
    A MultiMap is a Map with slightly different semantics. Putting a value into the map will add the value to a Collection at that key. Getting a value will return a Collection, holding all the values put to that key.
    
    For example:
    
     MultiMap mhm = new MultiHashMap();
     mhm.put(key, "A");
     mhm.put(key, "B");
     mhm.put(key, "C");
     Collection coll = (Collection) mhm.get(key);
    
    coll will be a collection containing "A", "B", "C". 
    

提交回复
热议问题