通过嵌套的hashmap迭代 [英] Iterate through nested hashmap
本文介绍了通过嵌套的hashmap迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将如何去遍历嵌套的HashMap?
HashMap
是这样设置的:
HashMap< String,HashMap< String,Student>>
其中学生
是一个包含变量的对象名称
。例如,如果我的HashMap看起来像这样(下面不是我的代码,它只是模拟hashmap的内容)
hm => HashMap<'S',Hashmap<'Sam',SamStudent>>
HashMap<'S',Hashmap<'Seb',SebStudent>>
HashMap<'T',Hashmap<'Thomas',ThomasStudent>>
如何遍历所有单字母键,然后遍历每个全名键,然后拉出(Map.Entry< String,HashMap<>)的名字?
解决方案
String,Student>> letterEntry:students.entrySet()){
String letter = letterEntry.getKey();
// ...
for(Map.Entry< String,Student> nameEntry:letterEntry.getValue()。entrySet()){
String name = nameEntry.getKey();
Student student = nameEntry.getValue();
// ...
}
}
How would I go about iterating through a nested HashMap?
The HashMap
is setup like this:
HashMap<String, HashMap<String, Student>>
Where Student
is an object containing a variable name
. If for instance my HashMap looked like this (the following is not my code, it's just to simulate what the contents of the hashmap could be)
hm => HashMap<'S', Hashmap<'Sam', SamStudent>>
HashMap<'S', Hashmap<'Seb', SebStudent>>
HashMap<'T', Hashmap<'Thomas', ThomasStudent>>
How could I iterate through all of the single letter keys, then each full name key, then pull out the name of the student?
解决方案
for (Map.Entry<String, HashMap<String, Student>> letterEntry : students.entrySet()) {
String letter = letterEntry.getKey();
// ...
for (Map.Entry<String, Student> nameEntry : letterEntry.getValue().entrySet()) {
String name = nameEntry.getKey();
Student student = nameEntry.getValue();
// ...
}
}
这篇关于通过嵌套的hashmap迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文