Best way to create a hashmap of arraylist

后端 未结 9 1065
小鲜肉
小鲜肉 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:38

    I Could not find any easy way. MultiMap is not always an option available. So I wrote something this.

    public class Context extends HashMap {
    
        public V addMulti(K paramK, V paramV) {
            V value = get(paramK);
            if (value == null) {
                List list = new ArrayList();
                list.add(paramV);
                put(paramK, paramV);
            } else if (value instanceof List) {
                ((List)value).add(paramV);
            } else {
                List list = new ArrayList();
                list.add(value);
                list.add(paramV);
                put(paramK, (V) list);
            }
            return paramV;
        }
    }
    

提交回复
热议问题