b遍历List
List<String> list1 = new ArrayList<String>();
list1.add("value1");
list1.add("value2");
list1.add("value3");
//遍历List
//底层用的是for循环
for(String value : list1) {
System.out.println(value);
}
//遍历List
for(int i=0;i<list1.size();i++) {
System.out.println(list1.get(i));
}
//遍历List
Iterator<String> it = list1.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
//遍历List
list1.forEach((value) -> {
System.out.println(value);
});
遍历Map
Map<String, Object> hashmap = new HashMap<String, Object>();
hashmap.put("key1", "value1");
hashmap.put("key2", "value2");
hashmap.put("key3", "value3");
//遍历Hashmap
Set<Entry<String, Object>> e = hashmap.entrySet();
for(Entry<String, Object> v : e) {
System.out.println(v);
}
//遍历hashmap
Iterator<Entry<String, Object>> it = hashmap.entrySet().iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
//遍历hashmap
for(String key : hashmap.keySet()) {
System.out.println(key+"="+hashmap.get(key));
}
//遍历Hasmap Lambda表达式
hashmap.forEach((key, value) -> {
System.out.println(key + ":" + value);
});