Java 8 Streams filter examples

Posted Blogger

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 8 Streams filter examples相关的知识,希望对你有一定的参考价值。

1. Streams filter() and collect()

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

}

}

Output


spring
node


2. Streams filter(), findAny() and orElse()
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);

}

}
Output


Person{name=‘jack‘, age=20}
null


2.3 For multiple condition.

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() and 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);

}

}
Output


name : jack

mkyong
jack
lawrence


源文
https://www.mkyong.com/java8/java-8-streams-filter-examples/



































































以上是关于Java 8 Streams filter examples的主要内容,如果未能解决你的问题,请参考以下文章

我可以在Java 8中复制Stream吗?

Java集合框架 流 Streams| Mapping| Filtering| Slicing | Sorting |Reducing | Collectors|...

Java 8 Streams - 分组为单个值[重复]

Java 8 中的 Streams API 详解

Hashmap with Streams in Java 8 Streams 收集 Map 的值

Java 8 Streams多个分组依据