Map集合的遍历
1、
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());
}
}
2、
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));
}
}
3、
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());
}
}
}
Set 集合的遍历代遍历
1、
Set<String> set = new HashSet<String>();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String str = it.next();
System.out.println(str);
}
2、
for (String str : set) {
System.out.println(str);
}
本文介绍了Java中Map集合的三种遍历方法:通过values()获取value集合进行遍历;通过keySet()获取key集合进行遍历;通过entrySet()获取键值对集合进行遍历。同时,还介绍了Set集合的两种遍历方式:使用迭代器进行遍历和使用增强for循环进行遍历。
313

被折叠的 条评论
为什么被折叠?



