HashMap with multiple values under the same key

前端 未结 22 1845
旧时难觅i
旧时难觅i 2020-11-22 06:49

Is it possible for us to implement a HashMap with one key and two values. Just as HashMap?

Please do help me, also by telling (if there is no way) any other way to

相关标签:
22条回答
  • 2020-11-22 07:07
    HashMap<Integer,ArrayList<String>> map = new    HashMap<Integer,ArrayList<String>>();
    
    ArrayList<String> list = new ArrayList<String>();
    list.add("abc");
    list.add("xyz");
    map.put(100,list);
    
    0 讨论(0)
  • 2020-11-22 07:07

    Just for the record, the pure JDK8 solution would be to use Map::compute method:

    map.compute(key, (s, strings) -> strings == null ? new ArrayList<>() : strings).add(value);
    

    Such as

    public static void main(String[] args) {
        Map<String, List<String>> map = new HashMap<>();
    
        put(map, "first", "hello");
        put(map, "first", "foo");
        put(map, "bar", "foo");
        put(map, "first", "hello");
    
        map.forEach((s, strings) -> {
            System.out.print(s + ": ");
            System.out.println(strings.stream().collect(Collectors.joining(", ")));
        });
    }
    
    private static <KEY, VALUE> void put(Map<KEY, List<VALUE>> map, KEY key, VALUE value) {
        map.compute(key, (s, strings) -> strings == null ? new ArrayList<>() : strings).add(value);
    }
    

    with output:

    bar: foo
    first: hello, foo, hello
    

    Note that to ensure consistency in case multiple threads access this data structure, ConcurrentHashMap and CopyOnWriteArrayList for instance need to be used.

    0 讨论(0)
  • 2020-11-22 07:08
     import java.io.*;
     import java.util.*;
    
     import com.google.common.collect.*;
    
     class finTech{
    public static void main(String args[]){
           Multimap<String, String> multimap = ArrayListMultimap.create();
           multimap.put("1","11");
           multimap.put("1","14");
           multimap.put("1","12");
           multimap.put("1","13");
           multimap.put("11","111");
           multimap.put("12","121");
            System.out.println(multimap);
            System.out.println(multimap.get("11"));
       }                                                                                            
     }                                                                    
    

    Output:

         {"1"=["11","12","13","14"],"11"=["111"],"12"=["121"]}
    
          ["111"]
    

    This is Google-Guava library for utility functionalities. This is the required solution.

    0 讨论(0)
  • 2020-11-22 07:09

    Take a look at Multimap from the guava-libraries and its implementation - HashMultimap

    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.

    0 讨论(0)
  • 2020-11-22 07:09

    I could not post a reply on Paul's comment so I am creating new comment for Vidhya here:

    Wrapper will be a SuperClass for the two classes which we want to store as a value.

    and inside wrapper class, we can put the associations as the instance variable objects for the two class objects.

    e.g.

    class MyWrapper {
    
     Class1 class1obj = new Class1();
     Class2 class2obj = new Class2();
    ...
    }
    

    and in HashMap we can put in this way,

    Map<KeyObject, WrapperObject> 
    

    WrapperObj will have class variables: class1Obj, class2Obj

    0 讨论(0)
  • 2020-11-22 07:14

    Can be done using an identityHashMap, subjected to the condition that the keys comparison will be done by == operator and not equals().

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