在这篇教程中我将向你展示 filter()
, collect()
, findAny()
和 orElse()
的用法
1. Streams filter() 和 collect()
1.1 在 Java 8 以前, 像下面这样过滤一个 List
:
BeforeJava8.java
package com.mkyong.java8;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BeforeJava8 {
public static void main(String[] args) {
List<string> lines = Arrays.asList("spring", "node", "mkyong");
List<string> result = getFilterOutput(lines, "mkyong");
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 (!"mkyong".equals(line)) { // we dont like mkyong
result.add(line);
}
}
return result;
}
}
Output
spring
node
1.2 Java 8里面的等价写法: 使用collect()
将一个List
转化为steam(流),然后使用stream.filter()
来过滤它.
NowJava8.java
package com.mkyong.java8;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class NowJava8 {
public static void main(String[] args) {
List<string> lines = Arrays.asList("spring", "node", "mkyong");
List<string> result = lines.stream() // 转化为一个流
.filter(line -> !"mkyong".equals(line)) // 排除 'mkyong'
.collect(Collectors.toList()); // 吧输出流收集回List中
result.forEach(System.out::println); //输出 : spring, node
}
}
Output
spring
node
2. Streams filter(), findAny() 和 orElse()
Person.java
package com.mkyong.java8;
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//gettersm setters, toString
}
2.1 在Java 8以前, 你想通过name获取一个Person
通常像这样 :
BeforeJava8.java
package com.mkyong.java8;
import java.util.Arrays;
import java.util.List;
public class BeforeJava8 {
public static void main(String[] args) {
List<person> persons = Arrays.asList(
new Person("mkyong", 30),
new Person("jack", 20),
new Person("lawrence", 40)
);
Person result = getStudentByName(persons, "jack");
System.out.println(result);
}
private static Person getStudentByName(List<person> persons, String name) {
Person result = null;
for (Person temp : persons) {
if (name.equals(temp.getName())) {
result = temp;
}
}
return result;
}
}
Output
Person{name='jack', age=20}
2.2 在Java 8里面的等价操作是使用 stream.filter()
来过滤 List
,使用 .findAny().orElse (null)
来返回指定的对象.
NowJava8.java
package com.mkyong.java8;
import java.util.Arrays;
import java.util.List;
public class NowJava8 {
public static void main(String[] args) {
List<person> persons = Arrays.asList(
new Person("mkyong", 30),
new Person("jack", 20),
new Person("lawrence", 40)
);
Person result1 = persons.stream() // 转化为流
.filter(x -> "jack".equals(x.getName())) // 只过滤出"jack"
.findAny() // 如果找到了就返回
.orElse(null); // 如果找不到就返回null
System.out.println(result1);
Person result2 = persons.stream()
.filter(x -> "ahmook".equals(x.getName()))
.findAny()
.orElse(null);
System.out.println(result2);
}
}
Output
Person{name='jack', age=20}
null
2.3 多重条件指定.
NowJava8.java
package com.mkyong.java8;
import java.util.Arrays;
import java.util.List;
public class NowJava8 {
public static void main(String[] args) {
List<person> persons = Arrays.asList(
new Person("mkyong", 30),
new Person("jack", 20),
new Person("lawrence", 40)
);
Person result1 = persons.stream()
.filter((p) -> "jack".equals(p.getName()) && 20 == p.getAge())
.findAny()
.orElse(null);
System.out.println("result 1 :" + result1);
//or like this
Person result2 = persons.stream()
.filter(p -> {
if ("jack".equals(p.getName()) && 20 == p.getAge()) {
return true;
}
return false;
}).findAny()
.orElse(null);
System.out.println("result 2 :" + result2);
}
}
Output
result 1 :Person{name='jack', age=20}
result 2 :Person{name='jack', age=20}
3. Streams filter() 和 map()
NowJava8.java
package com.mkyong.java8;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class NowJava8 {
public static void main(String[] args) {
List<person> persons = Arrays.asList(
new Person("mkyong", 30),
new Person("jack", 20),
new Person("lawrence", 40)
);
String name = persons.stream()
.filter(x -> "jack".equals(x.getName()))
.map(Person::getName) //吧流转化为String
.findAny()
.orElse("");
System.out.println("name : " + name);
List<string> collect = persons.stream()
.map(Person::getName)
.collect(Collectors.toList());
collect.forEach(System.out::println);
}
}
Output
name : jack
mkyong
jack
lawrence
注意
Highly recommend this Streams tutorial – Processing Data with Java SE 8 Streams
References
- Java 8 cyclic inference in my case
- Java 8 Explained: Using Filters, Maps, Streams and Foreach to apply Lambdas to Java Collections!
- Java 8 forEach examples
- [Java 8 Streams: multiple filters vs. complex condition][Java 8 Streams_ multiple filters vs. complex condition]
- Processing Data with Java SE 8 Streams
[Java 8 Streams_ multiple filters vs. complex condition]: http://stackoverflow.com/questions/24054773/java-8-streams-multiple-filters-vs-complex-condition