Best way to create a hashmap of arraylist

后端 未结 9 1048
小鲜肉
小鲜肉 2020-11-27 05:35

I have one million rows of data in .txt format. the format is very simple. For each row:

user1,value1
user2,value2
user3,value3
user1,value4
...

You k

9条回答
  •  一生所求
    2020-11-27 06:36

    Since Java 8 you can use map.computeIfAbsent

    https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#computeIfAbsent-K-java.util.function.Function-

    Collection values = map.computeIfAbsent(user, k -> new ArrayList<>());
    values.add(value);
    

提交回复
热议问题