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

前端 未结 6 627
轻奢々
轻奢々 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:48

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

    Map map = new HashMap<>();
    

    if you want to code the solution yourself, you need

    Map> map = new HashMap<>();
    

    Anyhow, the preffered way is to use a Guava Multimap

提交回复
热议问题