Java 8 Stream filter With Example

Java 8 Stream filter method has syntax as Stream<T> filter(Predicate<? superΒ T>Β predicate).

Predicate is a functional interface and can be used as the lambda expression or method reference. It accepts a expression which is true in nature. Therefore, filter method returns a stream having the elements of this stream which matches or satisfies the the given predicate.

This is theΒ intermediate operation.

Example – Java 8 Stream filter string with certain length

In this example I have shown how to filter string with length more than 3. Those names which have length more than three will remain while printing. However, the original stream will remain as it is.

Also, I am using forEach method to print each string object.

package StreamExample;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamFilterExample {

	public static void main(String[] args) {

		List<String> names = Arrays.asList("Rahul","Sam","Robert","Denial","Rachel");
			
		//Creating the stream of all names
		Stream<String> allNames = names.stream();
			
		//Creating another stream by filtering names having length more than 3 using filter()
		Stream<String> longNames = allNames.filter(str -> str.length() > 3);
			
		//displaying the filtered names
		longNames.forEach(str->System.out.print(str+" "));
	  }
}
##Output
Rahul Robert Denial Rachel

Example- Java 8 Stream filter used for filtering Objects

In this example, Person class is created and objects are filtered with certain age of 20.

package StreamExample;

public class Person 
{
	
	private String name;
	private int age;
	private String gender;
	private int salary;
	
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", gender=" + gender + ", salary=" + salary + "]";
	}
	public Person(String name, int age, String gender, int salary) {
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
		this.salary = salary;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}

	
}
package StreamExample;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamFilterExample {

	public static void main(String[] args) {

		List<Person> persons= Arrays.asList(new Person("Ashish",25,"Male",50000),
				new Person("Rita",22,"Female",40000),
				new Person("Yash",16,"Male",20000),
				new Person("Rahul",18,"Female",22000),
				new Person("Ram",27,"Male",39000));
		
		persons.stream().filter(p -> p.getAge() > 20).forEach(p -> System.out.println(p));
	  }
}
##Output
Person [name=Ashish, age=25, gender=Male, salary=50000]
Person [name=Rita, age=22, gender=Female, salary=40000]
Person [name=Ram, age=27, gender=Male, salary=39000]

Conclusion

Java 8 stream filter method accepts predicate which uses expression satisfying the true condition.

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply