在本教程中,我们将向您展示几个 java8 示例,以演示 Streams filter ()、 collect ()、 findAny ()和 orElse ()的使用。
Stream(流)是一个来自数据源的元素队列并支持聚合操作
和以前的 Collection 操作不同, Stream 操作还有两个基础的特征:
在 Java 8 中, 集合接口有两个方法来生成流:
1.java 8 之前过滤 list 使用如下方式:
List<String> lines = Arrays.asList("spring", "node", "php"); List<String> result = getFilterOutput(lines, "php"); for (String temp : result) { System.out.println(temp); //output : spring, node } } private static List<String> getFilterOutput(List<String> lines, String filter) { List<String> result = new ArrayList<>(); for (String line : lines) { if (!"php".equals(line)) { // we dont like php result.add(line); } } return result; }
2.java 8 之后,使用 stream.filter() 过滤 list,使用 collect() 将流转为 list.
List<String> lines = Arrays.asList("spring", "node", "php"); List<String> result = lines.stream() // convert list to stream .filter(line -> !"php".equals(line)) // we dont like php .collect(Collectors.toList()); // collect the output and convert streams to a List result.forEach(System.out::println); //output : spring, node
java8 之前,获取 name 通过如下方式:
public static void main(String[] args) { List<Developer> persons = Arrays.asList( new Developer("zhangsan", 20), new Developer("lisi",21), new Developer("wangwu",22)); Developer developer = getByNameBefore(persons,"lisi"); System.out.println(developer == null ? "不存在" : developer.toString()); } private static Developer getByNameBefore(List<Developer> persons, String lisi) { Developer result = null; for (Developer developer : persons) { if (developer.getName().equals(lisi)) { result = developer; } } return result; }
java8 之后:
public static void main(String[] args) { List<Developer> persons = Arrays.asList( new Developer("zhangsan", 20), new Developer("lisi",21), new Developer("wangwu",22)); Developer developer = persons.stream().filter(x -> "lisi".equals(x.getName())).findAny() .orElse(null); System.out.println(developer); // 不存在的 Developer developer1 = persons.stream().filter(x -> "asan".equals(x.getName())).findAny() .orElse(null); System.out.println(developer1); }
多条件查询使用:
public static void main(String[] args) { List<Developer> persons = Arrays.asList( new Developer("zhangsan", 20), new Developer("lisi",21), new Developer("wangwu",22)); Developer result1 = persons.stream() .filter(p -> "lisi".equals(p.getName()) && p.getAge() == 21).findAny().orElse(null); System.out.println(result1); Developer result2 = persons.stream().filter(p -> { if ("lisi".equals(p.getName()) && p.getAge() == 21) { return true; } return false; }).findAny().orElse(null); System.out.println(result2); }
public static void main(String[] args) { List<Developer> persons = Arrays.asList( new Developer("zhangsan", 20), new Developer("lisi",21), new Developer("wangwu",22)); String name = persons.stream().filter(x -> "lisi".equals(x.getName())) .map(Developer::getName) //convert stream to String .findAny().orElse(""); System.out.println("name : " + name); List<String> collect = persons.stream().filter(x -> "lisi".equals(x.getName())) .map(Developer::getName).collect(Collectors.toList()); collect.forEach(System.out::println); }
源码见: java-8-demo
系列文章详见: Java 8 教程