不叨叨,直接上菜
1、List遍历
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("7初级工程师");
list.add("6中级工程师");
list.add("5高级工程师");
list.add("4架程师");
list.add("3专家");
list.add("2首席技术官");
list.add("1首席技术官");
// 遍历1
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("-----------------华丽的分割线 普通for循环遍历----------------");
// 遍历2
for (String s:list) {
System.out.println(s);
}
System.out.println("-----------------华丽的分割线 增强for遍历----------------");
// 遍历3
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String next = iterator.next();
System.out.println(next);
}
System.out.println("-----------------华丽的分割线 Iterator迭代遍历----------------");
// 遍历4
list.stream().distinct().sorted().forEach(s -> System.out.println(s));
System.out.println("-----------------华丽的分割线 stream流遍历----------------");
}
2、Map遍历
public static void main(String[] args) {
Map<String,Object> map = new HashMap<>();
for (int j = 0; j <= 100; j++) {
map.put(""+j,"张飞"+j);
}
System.out.println("------------------华丽的分割线 entry对象遍历----------------");
// 遍历1
Set<Map.Entry<String, Object>> entries = map.entrySet();
for (Map.Entry<String, Object> s:entries) {
String key = s.getKey();
Object value = map.get(key);
System.out.println("key: " + key + " value: " + value);
}
System.out.println("------------------华丽的分割线 keyset遍历----------------");
// 遍历2
Set<String> keys = map.keySet();
for(String key : keys){
Object value = map.get(key);
System.out.println("key: " + key + " value: " + value);
}
System.out.println("------------------华丽的分割线 Iterator迭代遍历--------------");
// 遍历3
Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> next = iterator.next();
String key = next.getKey();
Object value = next.getValue();
System.out.println("key: " + key + " value: " + value);
}
System.out.println("-------------------华丽的分割线 所有的值遍历------------------");
// 遍历4
Collection<Object> values = map.values();
for (Object value : values) {
System.out.println(value);
}
}