Collection的遍历
Collection collection=new ArrayList<>();
collection.add('a');
collection.add('b');
//foreach增强遍历
for (Object object : collection) {
System.out.println(object);
}
//使用迭代器遍历
Iterator iterator = collection.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
Map的遍历
Map<String, String> map = new HashMap<>();
map.put("name", "李四");
map.put("age", "13");
//使用foreach增强
Set<Entry<String, String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
System.out.println(entry);
}
//使用迭代器
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext()) {
String key=iterator.next();
System.out.println(key+"="+map.get(key));
}