In this tutorial Java 8 Stream filter example, we will show you few Java 8 filter examples to demonstrate the use of Streams filter() and can be used with collect(), findAny(), orElse() and map(). The filter() is an intermediate operation that reads the data from a stream and returns a new stream after transforming the data based on the given condition.
A Simple Example of Java Stream Filter()
1 2 3 4 5 6 7 8 9 10 11 12 |
public static void main(String[] args) { List<String> language = Arrays.asList("Java", "NodeJs", "PHP"); //Creating the stream of all languages Stream<String> sLanguage = language.stream(); //Creating another stream by filtering all languages using filter() Stream<String> newLanguage = sLanguage.filter(str -> str.length() > 3); //displaying the all languages newLanguage.forEach(str->System.out.println(str)); } |
Output
1 2 |
Java NodeJs |
Example 2: Streams filter() and collect()
1 2 3 4 5 6 7 8 9 |
public static void main(String[] args) { List<String> lines = Arrays.asList("Java", "NodeJs", "PHP"); List<String> result = lines.stream() // convert list to stream .filter(line -> !"Java".equals(line)) // filter with condition .collect(Collectors.toList()); // collect the output and convert streams to a List result.forEach(System.out::println); } |
Output
1 2 |
NodeJs PHP |
Example 2: Streams filter(), findAny() and orElse()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public static void main(String[] args) { List<People> Peoples = Arrays.asList(new People(30,"M","Mr A"), new People(24,"M","Mr B"), new People(31,"M","Mr B")); People p1 = Peoples.stream() // Convert to steam .filter(x -> "Mr B".equals(x.getName())) // filter with condition .findAny() // If find any element then return found, first element is returned if found one more element .orElse(null); // If not found, return null System.out.println(p1); People p2 = Peoples.stream().filter(x -> "Mr C".equals(x.getName())).findAny().orElse(null); System.out.println(p2); } |
Output
1 2 |
Mr B Gender:M 24 year olds null |
Example 3: Stream filter() with multiple conditions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public static void main(String[] args) { List<People> Peoples = Arrays.asList(new People(30,"M","Mr A"), new People(24,"M","Mr B"), new People(31,"M","Mr B")); // filter with multiple condition People p3 = Peoples.stream().filter((p) -> "Mr B".equals(p.getName()) && 31 == p.getAge()).findAny() .orElse(null); System.out.println(p3); // or custom lambda expression like this People p4 = Peoples.stream().filter(p -> { if ("Mr B".equals(p.getName()) && 31 == p.getAge()) { return true; } return false; }).findAny().orElse(null); } |
Output
1 2 |
Mr B Gender:M 31 year olds Mr B Gender:M 31 year olds |
Example 4: Streams filter() and map()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public static void main(String[] args) { List<People> Peoples = Arrays.asList(new People(30, "M", "Mr B"), new People(24, "M", "Mr B"), new People(31, "M", "Mr A")); String strName = Peoples.stream().filter(x -> "Mr B".equals(x.getName())).map(People::getName) // convert stream to // String .findAny().orElse(""); System.out.println("Name : " + strName); List<String> strLst = Peoples.stream().map(People::getName).collect(Collectors.toList());// convert stream to // List of String strLst.forEach(System.out::println); } |
Output
That’s all about Java 8 Stream filter example.
Reference
Java 8 Stream
Processing Data with Java SE 8 Streams
Download complete source code, click link below
Java8StreamExample.zip (238 downloads)