java简化了foreach遍历。可以将list和mapzhuan转化为stream来操作
List<String> items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
items.add("E");
items.forEach(item->System.out.println(item));
items.forEach(System.out::println);
Map<String, Integer> items = new HashMap<>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);
items.forEach((k,v)->System.out.println("Item : " + k + " Count : " + v));
stream中的 filter map 方法都可以对stream进行操作。使用collect方法可以将stream转换为list和map
map将list中的字母转为大写
List<String> alpha = Arrays.asList("a", "b", "c", "d");
List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList());
filter 除去list中值为null的
Stream<String> language = Stream.of("java", "python", "node", null, "ruby", null, "php");
List<String> result = language.filter(x -> x!=null).collect(Collectors.toList());
List<String> result = language.filter(Objects::nonNull).collect(Collectors.toList());