如何用arraylist填充哈希图?

时间:2019-01-07 13:21:07

标签: java arraylist hashmap

这有点不言自明,但是我该如何填写呢?

Map<Integer,ArrayList<Integer>>intMap = new HashMap<Integer, ArrayList<Integer>>();

我已经尝试过

intMap.put(1, 2);
intMap.put(1, 3); etc

intMap.put(1, (2, 3);

5 个答案:

答案 0 :(得分:3)

您应该使用Map.computeIfAbsent

intMap.computeIfAbsent(someKey, k -> new ArrayList<>()).add(someValue);

例如,具有以下映射:

1 -> [2, 3]
5 -> [8]
6 -> [7, 9, 4]

您可以这样操作:

intMap.computeIfAbsent(1, k -> new ArrayList<>()).add(2);
intMap.computeIfAbsent(1, k -> new ArrayList<>()).add(3);

intMap.computeIfAbsent(5, k -> new ArrayList<>()).add(8);

intMap.computeIfAbsent(6, k -> new ArrayList<>()).add(7);
intMap.computeIfAbsent(6, k -> new ArrayList<>()).add(9);
intMap.computeIfAbsent(6, k -> new ArrayList<>()).add(4);

编辑:

Map.computeIfAbsent等效于以下代码:

List<Integer> list = intMap.get(someKey);
if (list == null) {
    list = new ArrayList<>();
    intMap.put(someKey, list);
}
list.add(someValue);

答案 1 :(得分:1)

List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
intMap.put(1, list);

list = new ArrayList<>();
list.add(4);
list.add(5);
list.add(6);
intMap.put(2, list);

答案 2 :(得分:1)

First, don't map to ArrayList. When using lists, never mention more specific than List except in the one place you construct it. There's no reason your map shouldn't accept a different type of List. (Also, if the order doesn't matter, it should just be a Collection, not a List. Declare things to be the least specific you can.)

The post above about using computeIfAbsent is the right answer while you are working with the map. If you're just trying to initialize the map with a few values, you could do this.

map.put(1, Arrays.asList(2,3));
map.put(2, Arrays.asList(7, 9, 11));

答案 3 :(得分:1)

If the map and the lists are not modifiable once instantiated, with Java 9 you could do :

Map<Integer, List<Integer>> map = Map.of(1, List.of(2, 3), 
                                         2, List.of(3, 5));

If the Map and the lists are modifiable, you could follow the approach of Federico Peralta Schaffner and makes it a little more user friendly by extracting a method with var-args :

void fill(Map<Integer, List<Integer>> map, Integer key, Integer... values) {
    map.computeIfAbsent(key, k -> new ArrayList<>())
       .addAll(Arrays.asList(values));
}

You can use it now easily :

Map<Integer, List<Integer>> map = new HashMap<>();
fill(map, 1, 2, 3);
fill(map, 2, 4, 6);
fill(map, 2, 1);

Beware, passing a null reference in the var-args will trigger a NullPointerException in Arrays.asList(values).

答案 4 :(得分:0)

您可以通过多种方式尝试简单

Map<Integer,ArrayList<Integer>>intMap = new HashMap<Integer, ArrayList<Integer>>();

intMap.put(101,new ArrayList<Integer>().add(1));
intMap.put(102,new ArrayList<Integer>().add(2));

我想如果每个索引中有多个值,那么只有您尝试这样做,在这种情况下,我们可以这样做,

ArrayList<Integer> list1=new ArrayList<Integer>();
list1.add(1);
list1.add(2);
...

ArrayList<Integer> list2=new ArrayList<Integer>();
list2.add(1);
list2.add(2);
...

然后

intMap.put(1,list1);
intMap.put(2,list2);