How to have a key with multiple values in a map?

前端 未结 6 606
轻奢々
轻奢々 2020-12-10 17:10

I have a map like this

Map map=new HashMap();//HashMap key random order.
map.put(\"a\",10);
map.put(\"a\",20);
map.put(\"a\",30);
map.put(\"b\",10);

System.         


        
相关标签:
6条回答
  • 2020-12-10 17:40

    Put an ArrayList instance in the value part.

    void addValue(Map map, Object key, Object value) {
        Object obj = map.get(key);
        List list;
        if (obj == null) {  
            list = new ArrayList<Object>();  
        } else {
            list = ((ArrayList) obj);
        }
        list.add(value);
        map.put(key, list);
    }
    

    For More Info check this.

    0 讨论(0)
  • 2020-12-10 17:44

    To implement what you want using the Java standard library, I would use a map like this:

    Map<String, Collection<Integer>> multiValueMap = new HashMap<String, Collection<Integer>>();
    

    Then you can add values:

    multiValueMap.put("a", new ArrayList<Integer>());
    multiValueMap.get("a").add(new Integer(10));
    multiValueMap.get("a").add(new Integer(20));
    multiValueMap.get("a").add(new Integer(30));
    

    If this results uncomfortable for you, consider wrapping this behaviour in a dedicated Class, or using a third-party solution, as others have suggested here (Guava Multimap).

    0 讨论(0)
  • 2020-12-10 17:48

    You shouldn't ignore the generic parameters. What you have is

    Map<String, Integer> map = new HashMap<>();
    

    if you want to code the solution yourself, you need

    Map<String, List<Integer>> map = new HashMap<>();
    

    Anyhow, the preffered way is to use a Guava Multimap

    0 讨论(0)
  • 2020-12-10 17:51

    For a Map with entries with same key, has no sense to use get() .But as long as you use iterator() or entrySet() this should work:

    class HashMap<String, String> {
    
      Set<Entry<String, String>> entries;
      @Override
      public Set<Entry<String, String>> entrySet() {
        return entries;
      }
    
      @Override
      public int size() {
        return entries.size();
      }
    
      public String put(String key, String value) {
        if (entries == null) {
          entries = new AbstractSet<Entry<String, String>>() {
    
            ArrayList<Entry<String, String>> list = new ArrayList<>();
            @Override
            public Iterator<Entry<String, String>> iterator() {
              return list.iterator();
            }
    
            @Override
            public int size() {
              return list.size();
            }
    
            @Override
            public boolean add(Entry<String, String> stringStringEntry) {
              return list.add(stringStringEntry);
            }
          };
        }
        StatusHandler.MyEntry entry = new StatusHandler.MyEntry();
        entry.setKey(key);
        entry.setValue(value);
        entries.add(entry);
        return value;
      }
    };
    

    TL;DR So, what is it useful for? That comes from a hack to redmine-java-api to accept complex queries based on form params:

    https://stackoverflow.com/a/18358659/848072

    https://github.com/albfan/RedmineJavaCLI/commit/2bc51901f2f8252525a2d2258593082979ba7122

    0 讨论(0)
  • 2020-12-10 17:52

    Have you checked out Guava Multimaps ?

    A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.

    If you really want to use standard collections (as suggested below), you'll have to store a collection per key e.g.

    map = new HashMap<String, Collection<Integer>>();
    

    Note that the first time you enter a new key, you'll have to create the new collection (List, Set etc.) before adding the first value.

    0 讨论(0)
  • 2020-12-10 17:52

    Use Map with value type as list of values..For example, in your map, while adding an entry, you will put key as "a" and you will have to add it's value as a list of Integer , having all the required values, like 1,2,3,4.

    0 讨论(0)
提交回复
热议问题