Best way to create a hashmap of arraylist

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

    The ArrayList values in your HashMap are references. You don't need to "put it back to HashMap". You're operating on the object that already exists as a value in the HashMap.

    0 讨论(0)
  • 2020-11-27 06:15

    As already mentioned, MultiMap is your best option.

    Depending on your business requirements or constraints on the data file, you may want to consider doing a one-off sorting of it, to make it more optimised for loading.

    0 讨论(0)
  • 2020-11-27 06:21

    Use Multimap from Google Collections. It allows multiple values for the same key

    https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/Multimap.html

    0 讨论(0)
  • 2020-11-27 06:26

    i think what you want is the Multimap. You can get it from apache's commons collection, or google-collections.

    http://commons.apache.org/collections/

    http://code.google.com/p/google-collections/

    "collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values."

    0 讨论(0)
  • 2020-11-27 06:29

    You don't need to re-add the ArrayList back to your Map. If the ArrayList already exists then just add your value to it.

    An improved implementation might look like:

    Map<String, Collection<String>> map = new HashMap<String, Collection<String>>();
    

    while processing each line:

    String user = user field from line
    String value = value field from line
    
    Collection<String> values = map.get(user);
    if (values==null) {
        values = new ArrayList<String>();
        map.put(user, values)
    }
    values.add(value);
    

    Follow-up April 2014 - I wrote the original answer back in 2009 when my knowledge of Google Guava was limited. In light of all that Google Guava does, I now recommend using its Multimap instead of reinvent it.

    Multimap<String, String> values = HashMultimap.create();
    values.put("user1", "value1");
    values.put("user2", "value2");
    values.put("user3", "value3");
    values.put("user1", "value4");
    
    System.out.println(values.get("user1"));
    System.out.println(values.get("user2"));
    System.out.println(values.get("user3"));
    

    Outputs:

    [value4, value1]
    [value2]
    [value3]
    
    0 讨论(0)
  • 2020-11-27 06:31

    it would be faster if you used a LinkedList instead of an ArrayList, as the ArrayList will need to resize when it nears capacity.

    you will also want to appropriately estimate the capacity of the wrapping collection (HashMap or Multimap) you are creating to avoid repetitive rehashing.

    0 讨论(0)
提交回复
热议问题