在本教程中,我们将向您展示一些Java 8示例,以演示Streams filter()
, collect()
, findAny()
和orElse()
1.流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;
}
}
输出量
spring
node
1.2 Java 8中的等效示例,用stream.filter()
过滤List
,并用collect()
将流转换为List
。
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() // convert list to stream
.filter(line -> !"mkyong".equals(line)) // we dont like mkyong
.collect(Collectors.toList()); // collect the output and convert streams to a List
result.forEach(System.out::println); //output : spring, node
}
}
输出量
spring
node
2.流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之前,您可以通过以下方式获得一个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;
}
}
输出量
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() // Convert to steam
.filter(x -> "jack".equals(x.getName())) // we want "jack" only
.findAny() // If 'findAny' then return found
.orElse(null); // If not found, return null
System.out.println(result1);
Person result2 = persons.stream()
.filter(x -> "ahmook".equals(x.getName()))
.findAny()
.orElse(null);
System.out.println(result2);
}
}
输出量
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);
}
}
输出量
result 1 :Person{name='jack', age=20}
result 2 :Person{name='jack', age=20}
3.流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) //convert stream to String
.findAny()
.orElse("");
System.out.println("name : " + name);
List<String> collect = persons.stream()
.map(Person::getName)
.collect(Collectors.toList());
collect.forEach(System.out::println);
}
}
输出量
name : jack
mkyong
jack
lawrence
注意
强烈推荐此Streams教程– 使用Java SE 8 Streams处理数据
参考文献
- 在我的情况下,Java 8循环推断
- Java 8简介:使用过滤器,地图,流和Foreach将Lambda应用于Java集合!
- Java 8 forEach示例
- Java 8流:多个过滤器与复杂条件
- 使用Java SE 8流处理数据
翻译自: https://mkyong.com/java8/java-8-streams-filter-examples/