Map implementation with duplicate keys

后端 未结 18 1732
暗喜
暗喜 2020-11-22 15:23

I want to have a map with duplicate keys.

I know there are many map implementations (Eclipse shows me about 50), so I bet there must be one that allows this. I know

相关标签:
18条回答
  • 2020-11-22 15:53

    You could simply pass an array of values for the value in a regular HashMap, thus simulating duplicate keys, and it would be up to you to decide what data to use.

    You may also just use a MultiMap, although I do not like the idea of duplicate keys myself.

    0 讨论(0)
  • 2020-11-22 15:54
    Multimap<Integer, String> multimap = ArrayListMultimap.create();
    
    multimap.put(1, "A");
    multimap.put(1, "B");
    multimap.put(1, "C");
    multimap.put(1, "A");
    
    multimap.put(2, "A");
    multimap.put(2, "B");
    multimap.put(2, "C");
    
    multimap.put(3, "A");
    
    System.out.println(multimap.get(1));
    System.out.println(multimap.get(2));       
    System.out.println(multimap.get(3));
    

    Output is:

    [A,B,C,A]
    [A,B,C]
    [A]
    

    Note: we need to import library files.

    http://www.java2s.com/Code/Jar/g/Downloadgooglecollectionsjar.htm

    import com.google.common.collect.ArrayListMultimap;
    import com.google.common.collect.Multimap;
    

    or https://commons.apache.org/proper/commons-collections/download_collections.cgi

    import org.apache.commons.collections.MultiMap;
    import org.apache.commons.collections.map.MultiValueMap;
    
    0 讨论(0)
  • 2020-11-22 15:54
     1, Map<String, List<String>> map = new HashMap<>();
    

    this verbose solution has multiple drawbacks and is prone to errors. It implies that we need to instantiate a Collection for every value, check for its presence before adding or removing a value, delete it manually when no values are left, etcetera.

    2, org.apache.commons.collections4.MultiMap interface
    3, com.google.common.collect.Multimap interface 
    

    java-map-duplicate-keys

    0 讨论(0)
  • 2020-11-22 15:55

    You are searching for a multimap, and indeed both commons-collections and Guava have several implementations for that. Multimaps allow for multiple keys by maintaining a collection of values per key, i.e. you can put a single object into the map, but you retrieve a collection.

    If you can use Java 5, I would prefer Guava's Multimap as it is generics-aware.

    0 讨论(0)
  • 2020-11-22 15:55

    No fancy libs required. Maps are defined by a unique key, so dont bend them, use a list. Streams are mighty.

    import java.util.AbstractMap.SimpleImmutableEntry;
    
    List<SimpleImmutableEntry<String, String>> nameToLocationMap = Arrays.asList(
        new SimpleImmutableEntry<>("A", "A1"),
        new SimpleImmutableEntry<>("A", "A2"),
        new SimpleImmutableEntry<>("B", "B1"),
        new SimpleImmutableEntry<>("B", "B1"),
    );
    

    And thats it. Usage examples:

    List<String> allBsLocations = nameToLocationMap.stream()
            .filter(x -> x.getKey().equals("B"))
            .map(x -> x.getValue())
            .collect(Collectors.toList());
    
    nameToLocationMap.stream().forEach(x -> 
    do stuff with: x.getKey()...x.getValue()...
    
    0 讨论(0)
  • 2020-11-22 15:56

    Learn from my mistakes...please don't implement this on your own. Guava multimap is the way to go.

    A common enhancement required in multimaps is to disallow duplicate keys-value pairs.

    Implementing/changing this in a your implementation can be annoying.

    In Guava its as simple as:

    HashMultimap<String, Integer> no_dupe_key_plus_val = HashMultimap.create();
    
    ArrayListMultimap<String, Integer> allow_dupe_key_plus_val = ArrayListMultimap.create();
    
    0 讨论(0)
提交回复
热议问题