java Map 怎么遍历
程序员文章站
2022-06-10 12:21:31
...
//最常规的一种遍历方法,最常规就是最常用的,虽然不复杂,但很重要,这是我们最熟悉的,就不多说了!!
public static void work(Map<String, Student> map) {
Collection<Student> c = map.values();
Iterator it = c.iterator();
for (; it.hasNext();) {
System.out.println(it.next());
}
}
//利用keyset进行遍历,它的优点在于可以根据你所想要的key值得到你想要的 values,更具灵活性!!
public static void workByKeySet(Map<String, Student> map) {
Set<String> key = map.keySet();
for (Iterator it = key.iterator(); it.hasNext();) {
String s = (String) it.next();
System.out.println(map.get(s));
}
}
//比较复杂的一种遍历在这里,呵呵~~他很暴力哦,它的灵活性太强了,想得到什么就能得到什么~~
public static void workByEntry(Map<String, Student> map) {
Set<Map.Entry<String, Student>> set = map.entrySet();
for (Iterator<Map.Entry<String, Student>> it = set.iterator(); it.hasNext();) {
Map.Entry<String, Student> entry = (Map.Entry<String, Student>) it.next();
System.out.println(entry.getKey() + "--->" + entry.getValue());
}
}
}
reference link: http://zhidao.baidu.com/question/283029340.html&__bd_tkn__=65b24a6e743cde375417e377aef52fa39411bbed8078338d51fed8133ea5c69d362ad36bb4bcda3b39bb3949f6bbe47087ac3af56e60b1f4e7eb60157a54fe329f6eaafc5e0f03de0125277ed537cb7d3a749e057822c888d23f427d72203021ce1408454fc5a4a29f08feaccbdc8d03ca3023f64daf
上一篇: 我今年二十七八岁