您应该考虑使用列表列表而不是2D数组,因为我确定行和列将非常精确,而且您可能不会提前知道每个列的初始大小。
我做了一些假设,因为你没有指定。您可以根据需要进行修改以适用于您的特定场景。见下面的假设。
假设
- 的 “琴弦” 你在乎即 “动物”, “人” 是你
hashMap
的钥匙。
- 领域
a
在Node
类类型的String
- 你关心
实现你有一个列表中的所有字符串
public static void main(String[] args) throws URISyntaxException, IOException {
Map<String, LinkedList<Node>> hashMap = new HashMap<String, LinkedList<Node>>();
List<List<String>> multiDemList = new ArrayList<List<String>>(); //Once the method is done this will contain your 2D list
List<String> needInfoOn = new ArrayList<String>(); //This should contain all of the HashMap Keys you are interested in i.e. Animal, Human keys
for(String s: needInfoOn){
if(!hashMap.containsKey(s)) continue; //if the map doesnt contain this string then skip to the next so we dont add empty rows in our multidimensional array. remove this line if you want empty rows
List<String> list = BuildTypeAList(hashMap, s);
multiDemList.add(list);
}
}
private static List<String> BuildTypeAList(Map<String, LinkedList<Node>> map, String s) {
LinkedList<Node> linkedList = map.get(s);
ArrayList<String> arrList = new ArrayList<String>();
for(Node n: linkedList) {
arrList.add(n.a);
}
return arrList;
}
private static class Node {
String a;
String b;
}