HashMap with multiple values under the same key

前端 未结 22 2068
旧时难觅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:08

     import java.io.*;
     import java.util.*;
    
     import com.google.common.collect.*;
    
     class finTech{
    public static void main(String args[]){
           Multimap 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.

提交回复
热议问题