In Java8 functional style, how can i map the values to already existing key value pair

前端 未结 4 817
抹茶落季
抹茶落季 2020-11-29 12:27

I have a map:

Map> dataMap;

Now i want to add new key value pairs to the map like below:

i         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 12:49

    This can be simplified by using the ternary operator. You don't really need the if-else statement

    List list = dataMap.containsKey(key) ?  dataMap.get(key) : new ArrayList<>();
    list.add(someNewObject);
    dataMap.put(key, list);
    
        

    提交回复
    热议问题