正如标题所说,我想知道是否存在任何可以允许这样的结构:
key = 1 -> arrayList1 、 arrayList2 、 arrayList3...
someStructure.add(1,arrayList<1>);
someStructure.add(1,arrayList<2>);
someStructure.add(1,arrayList<3>);
重要
每个列表的键必须保持相同。
最佳答案
您可以使用 Map
自行推出
Map<YourKeyClass, List<Data>> someStructure
= new HashMap<YourKeyClass, List<Data>>();
根据您想要的情况,看起来您需要:
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,它的行为更像你想要/需要的。
更多信息:
关于java - 在单个结构中插入多个 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23582585/