Map集合的4种遍历:
方法一:keySet+for循环的形式
Set<String> keySet = map.keySet();for (String key : keySet) {
String _value = map.get(key);
System.out.println("键:" + key + ",值:" + _value);
}
方法二:keySet+iterator
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String _value = map.get(key);
System.out.println("键:" + key + ",值:" + _value);
}
方法三:entrySet()+for循环
Set<Entry<String, String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
String key = entry.getKey();
String _value = entry.getValue();
System.out.println("键:" + key + ",值:" + _value);
}
方法四:
Iterator<Entry<String, String>> entryIterator = entrySet.iterator();
while (iterator.hasNext()) {
Entry<String, String> 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; x<list.size(); x++) {
String s =(String) list.get(x);
System.out.println(s);
}