Why predicate in java 8

As per our recent discussion on Predicate, Now you have some knowledge of the Predicate interface. In this article, we will read why predicate in java 8? and how we use java predicate example in real-time.
As you already know Predicate interface evaluates the condition and returns the result in boolean form. In the Predicate interface, we have an abstract method test(T t). Whenever you are using the Predicate interface then you must have to provide the body to test(T t) method. You can implement it by using lambda expression or by using the implements keyword.

By using a Predicate interface the lambda expression provides optimized code. In Java 8, Stream has some methods that expect predicate as input so that they can produce output. Let’s see the java predicate example

Stream<T> filter(Predicate<? super T> predicate); 

It accepts the predicate as input and filters the objects according to the condition defined in the predicate.

java predicate example

Use of Predicate in Real-life example

Let’s say you have a record of colleges and want to print the name of the college which has a Rank of less than 4. So, you must have to check the Rank of each college. So, we will do this task with two approaches.
The first approach goes without predicate and the second with the predicate.

Here is the first approach without the predicate

public class College 
{
	String name;
	int rank;
	String city;
	
	public College(String name, int rank, String city) 
	{
		this.name = name;
		this.rank = rank;
		this.city = city;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getRank() {
		return rank;
	}
	public void setRank(int rank) {
		this.rank = rank;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
}
import java.util.ArrayList;
import java.util.List;

public class ExampleWithoutPredicate {

	public static void main(String[] args) 
	{
		College ClgOne = new College("MLN", 1, "YamunaNagar");
		College clgTwo = new College("KUK", 4, "KKR");
		College clgThree = new College("JJP", 3, "Delhi");
		College clgFour = new College("KKL", 6, "UP");
		College clgFive = new College("DPS", 8, "Ambala");
		College clgSix = new College("JNU", 2, "J&K");
		
		List<College> colleges = new ArrayList<College>();
		colleges.add(ClgOne);
		colleges.add(clgTwo);
		colleges.add(clgThree);
		colleges.add(clgFour);
		colleges.add(clgFive);
		colleges.add(clgSix);
		
		// Filter and print the college have rank less than 4 
		System.out.println("Filtering and Printing by use of loop");
		for(College clg : colleges)
		{
			if(clg.getRank() < 4)
			{
				System.out.println("College Name: "+ clg.getName());
			}
		}
	}
}

Output: Filtering and Printing by use of loop
College Name: MLN
College Name: JJP
College Name: JNU

In the above example, we are filtering and printing the college name by use of the If statement.

Here is the second approach with the predicate

public class College 
{
	String name;
	int rank;
	String city;
	
	public College(String name, int rank, String city) 
	{
		this.name = name;
		this.rank = rank;
		this.city = city;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getRank() {
		return rank;
	}
	public void setRank(int rank) {
		this.rank = rank;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
}
import java.util.ArrayList;
import java.util.List;

public class ExampleWithPredicate 
{
	public static void main(String[] args) 
	{
		College ClgOne = new College("MLN", 1, "YamunaNagar");
		College clgTwo = new College("KUK", 4, "KKR");
		College clgThree = new College("JJP", 3, "Delhi");
		College clgFour = new College("KKL", 6, "UP");
		College clgFive = new College("DPS", 8, "Ambala");
		College clgSix = new College("JNU", 2, "J&K");
				
		List<College> colleges = new ArrayList<College>();
		colleges.add(ClgOne);
		colleges.add(clgTwo);
		colleges.add(clgThree);
		colleges.add(clgFour);
		colleges.add(clgFive);
		colleges.add(clgSix);
				
		// Filter the college have rank less than 4 
		System.out.println("Filtering and Printng by use of loop");
		colleges.stream().filter(clg -> (clg.getRank() < 4)).forEach((clg) -> System.out.println("College Name: "+clg.getRank()));	
	}

}

Output: Filtering and Printing by use of loop
College Name: 1
College Name: 3
College Name: 2

In this example filtering and printing the college names by use of Streams. In JAVA, Streams have a filter method that takes Predicate as input and returns the result.

So now you can see we are filtering and printing all the data in one line. So, it makes code more readable and optimized.

4 thoughts on “Why predicate in java 8”

  1. First of all, allow my family recognize a persons command during this matter. Even though this is certainly brand new , nevertheless soon after registering your site, this intellect has exploded extensively. Allow all of us to take hold of ones rss to help keep in touch with at all probable messages Sincere understand but will pass it on to help admirers and my personal are living members

    Reply

Leave a Comment