在单个结构中插入许多 ArrayList [英] inserting many ArrayList inside a single structure
本文介绍了在单个结构中插入许多 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正如标题所说,我想知道是否存在任何可以允许这样的结构:
as title says i want to know if exist any structure that can allows something like this:
key = 1 -> arrayList1、arrayList2、arrayList3...
someStructure.add(1,arrayList<1>);
someStructure.add(1,arrayList<2>);
someStructure.add(1,arrayList<3>);
重要
键对于每个列表必须保持相同.
the key has to remain the same for each list.
推荐答案
您可以使用 Map
Map<YourKeyClass, List<Data>> someStructure
= new HashMap<YourKeyClass, List<Data>>();
对于你想要的情况,看起来你需要:
To your desired situation, looks like you need:
Map<YourKeyClass, List<List<Data>>> someStructure
= new HashMap<YourKeyClass, List<List<Data>>>();
这是结构的启动示例:
class MyMapOfList<K, V> {
Map<K, List<V>> innerMap;
public MyMapOfListOfList() {
innerMap = new HashMap<K, V>();
}
public void put(K key, V value) {
List<V> list = innerMap.get(key);
if (list == null) {
list = new ArrayList<V>();
innerMap.put(key, list);
}
list.add(value);
}
}
你可以这样使用它:
MyMapOfList<String, List<Data>> myMapOfList;
如果您不喜欢在 Map
内部添加/维护 List
,您可以使用第三方库中的结构,例如 MultiMap
来自 Guava,它的行为更像您想要/需要的.
If you don't like to add/maintain the List
s internally in the Map
, you could use a structure from third party library like MultiMap
from Guava, which behaves more like you want/need.
更多信息:
这篇关于在单个结构中插入许多 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文