Stream API Filter examples with code samples
In this blog post, I will be demonstrating the filter method provided by Java 8 Stream API.In order to understand the Stream API in detail, refer to this blog post.
Introduction
The filter method in the Stream API can be applied on an existing Stream. It creates a new Stream with all the elements that match the condition specified in the filter method. It accepts a Predicate instance which is an in-built functional interface. Refer this blog post for a detailed Predicate example.
Code Sample with Integer
public class FilterStreamDemo { public static void main(String[] args) { List<Integer> input = Arrays.asList(5, 3, 11, 15, 9, 2, 5, 11); Stream<Integer> stream = input.stream().filter(num -> num >= 9); stream.forEach(num -> System.out.println(num)); }}
This code uses the filter method to obtain all the elements that are greater than 9. It first obtains a stream instance on the input ArrayList using the stream() method. Then the code invokes the filter method on the stream instance. Here, the Predicate is implemented via a lambda expression. So the code passes each element in the input list to the Predicate, checks if the number is greater than 9 and if so adds it to the new Stream.
When you execute this code, it will print the following output:
1115911
Code Sample with String
The following code demonstrates the filter method with a String ArrayList as input:
public class FilterStringDemo { public static void main(String[] args) { List<String> strList = Arrays.asList("Apple","Orange","Mango","Banana"); Stream<String> filteredStream = strList.stream().filter( str -> str.startsWith("M")); filteredStream.forEach(str -> System.out.println(str)); }}
Here, the filter method is used to find the list of fruits in the input list that starts with an “M“. So when you execute this code, it prints the following output:
Mango
Code Sample with any Class
You can also use the filter method on your own custom classes. For example. consider the following Animal class:
public class Animal { private String name; private String type; public Animal(String name, String type) { super(); this.name = name; this.type = type; } //getters and setters}
This code defines a class Animal with two fields, “name” and “type“. The name signifies the name of the Animal and type signifies whether the Animal is a herbivore or carnivore. Now consider the following code that creates a List of Animals and uses the filter method:
public class FilterAnimalDemo { public static void main(String[] args) { List<Animal> animals = new ArrayList<Animal>(); animals.add(new Animal("cow","herbivore")); animals.add(new Animal("lion","carnivore")); animals.add(new Animal("tiger","carnivore")); animals.add(new Animal("giraffe","herbivore")); animals.add(new Animal("zebra","herbivore")); Stream<Animal> herbivores = animals.stream().filter(animal -> animal.getType().equals("herbivore")); herbivores.forEach(animal -> System.out.println(animal.getName())); }}
This code uses the filter method to find all the animals that are Herbivores. So when you execute this code, it will print the following output:
cowgiraffezebra
You can get the source code for this example along with the code for other Java 8 examples at the Github repository here.
Comments
Post a Comment