map集合的4种遍历方法和List集合的2种遍历和迭代器Iterator的使用

Map集合的4种遍历:

  方法一:keySet+for循环的形式

Set keySet = map.keySet();
for (String key : keySet) {
String _value = map.get(key);
System.out.println("键:" + key + ",值:" + _value);

}

方法二:keySet+iterator

Iterator iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String _value = map.get(key);
System.out.println("键:" + key + ",值:" + _value);


}
方法三:entrySet()+for循环
Set> entrySet = map.entrySet();
for (Entry entry : entrySet) {
String key = entry.getKey();
String _value = entry.getValue();
System.out.println("键:" + key + ",值:" + _value);


}
方法四:
Iterator> entryIterator = entrySet.iterator();
while (iterator.hasNext()) {
Entry entry = entryIterator.next();
String key = entry.getKey();
String _value = entry.getValue();
System.out.println("键:" + key + ",值:" + _value);
}

Collection的2种遍历:

Iterator it = list.iterator();
while(it.hasNext()) {
String s =(String) it.next();
System.out.println(s);
}


for(int x=0; xString s =(String) list.get(x);
System.out.println(s);
}


你可能感兴趣的:(map集合的4种遍历方法和List集合的2种遍历和迭代器Iterator的使用)