用java创建多维数组列表

用java创建多维数组列表,java,Java,我想创建一个arrayList,如下所示 id->1512 associated with the values -> 12,45,78 id->1578 associated with the values -> 456,78,87,96 我该怎么办?我应该创建二维arrayList还是可以使用一维arrayList创建二维arrayList?您正在寻找类似以下内容: Map<Integer, List<Integer>> Map 您

我想创建一个arrayList,如下所示

id->1512   associated with the values -> 12,45,78
id->1578   associated with the values -> 456,78,87,96

我该怎么办?我应该创建二维arrayList还是可以使用一维arrayList创建二维arrayList?

您正在寻找类似以下内容:

Map<Integer, List<Integer>>
Map

您不需要ArrayList。请阅读,使用,您可以为您的关联执行以下操作:

Multimap<Integer, Integer> map = HashMultimap.create();
map.putAll(1512, Arrays.asList(12, 45, 78));
map.putAll(1578, Arrays.asList(456, 78, 87, 96));

这里有一个指向Guava的链接

您需要一个
多地图
(例如来自Guava),或者一个
地图
我更喜欢一个我不喜欢使用第三方的地图libraries@Aubin:为什么不呢?
MultiMap
将产生更好的代码,因为您不必检查每个键。@jlordo谢谢,但以前从未真正使用过Guava库P.但听起来不错:)@PremGenError:将我答案中的代码与将键/值对添加到你的
地图中所需的代码进行比较。
@Aubin:对不起,我不明白你的评论对我的答案的意思。Occam的剃刀说你必须根据目标调整方法。对于这个简单的问题来说,番石榴太大了。依赖关系降低了解决方案的可重用性。@Aubin:好的,现在有意义了。在这种情况下,我们必须同意不同意,因为番石榴使编程这种地图变得容易得多。插入时不必
null
检查,也不必考虑使用什么样的集合来保存值等等。
int key = 1512;
for (Integer value : map.get(key)) {
    System.out.println("Associated " + key + " -> " + value);
}