In the previous tutorial, we learned about Java Stream. I would recommend you to read that guide before going through this tutorial. In this guide, we will discuss the Java stream filter. 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. Lets take a simple example first and then we will see the examples of stream filter with other methods of the stream.
A Simple Example of Java Stream Filter()
In this example we are creating a stream from the list of names using stream()
method and then we are creating another stream of long names using stream filter(). As I mentioned above, the stream filter transforms the data of one stream into another stream.
import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class Example { public static void main(String[] args) { List<String> names = Arrays.asList("Melisandre","Sansa","Jon","Daenerys","Joffery"); //Creating the stream of all names Stream<String> allNames = names.stream(); //Creating another stream by filtering long names using filter() Stream<String> longNames = allNames.filter(str -> str.length() > 6); //displaying the long names longNames.forEach(str->System.out.print(str+" ")); } }
Output:
Melisandre Daenerys Joffery
Lets take few more examples of Java stream filter.
Example 1: Stream filter() and collect()
We can create a stream and apply a filter in a one line as shown in the example below. The collect() method here collects the final stream and converts it into a list
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<String> names = Arrays.asList("Melisandre","Sansa","Jon","Daenerys","Joffery"); List<String> longnames = names.stream() // converting the list to stream .filter(str -> str.length() > 6) // filter the stream to create a new stream .collect(Collectors.toList()); // collect the final stream and convert it to a List longnames.forEach(System.out::println); } }
Output:
Melisandre Daenerys Joffery
Example 2: Stream filter() with multiple conditions
In the above examples we have seen that there is only one condition in the filter() method. We can have more than one conditions in the filter() method joined using the logical operators in java. In the following example, we have two conditions in the filter method joined using and (&&) logical operator.
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<String> names = Arrays.asList("Melisandre","Sansa","Jon","Daenerys","Joffery"); List<String> longnames = names.stream() .filter(str -> str.length() > 6 && str.length() < 8) //Multiple conditions .collect(Collectors.toList()); longnames.forEach(System.out::println); } }
Output:
Joffery
Example 3: Stream filter() and map() method in Java
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<Integer> num = Arrays.asList(1,2,3,4,5,6); List<Integer> squares = num.stream() .map(n -> n * n) .collect(Collectors.toList()); System.out.println(squares); } }
Output:
[1, 4, 9, 16, 25, 36]
Leave a Reply