Using built-in Function interfaces of java.util.function pacakge in JAVA 8- Functional Programming in JAVA 8- Part 5

Java

Before reading this post kindly go from part 4 of this post to understand lambda expression.

http://jkoder.com/java-7-way-vs-java-8-way-lets-get-our-hands-dirty-with-lambdas-functional-programming-in-java-8-part-4/

Previously we saw JAVA 7 way & JAVA 8 way of doing things. One thing that we feel is extra is the interface.

interface Condition{
	public boolean test(Person p);
}

The Condition interface has a boolean method which took in Person object. You can also rename it to “Foo”, it doesn’t really matter what the name of the interface is & what the name of the method is. What important is the signature of the abstract method and return type of abstract method, this is used for type inference. This user-defined interface is a kind of overhead. It would be really good if you somehow just create a lambda expression & somehow there is a type for it. Guess what! you can do that using java.util.function package.

There are all these common patterns(abstract method signature and return type) for which JAVA language designers have already created interfaces in java.util.function package. For our Condition interface which has a method which takes a person object & returns boolean, so likewise there is a Predicate interface which has a test method which takes a generic type object & returns a boolean, so this can be of our use.

So, if you ever need a lambda expression that takes in a object and returns a boolean, then you don’t have to create a new interface, you can use the Predicate interface test method of java.util.function package. Let’s rewrite our previous code with Predicate interface.

public class ExerciseSolutionJava8 {

	public static void main(String[] args) {
		List<Person> people=Arrays.asList(
				new Person("Charles","Dickens",60),
				new Person("Lewis","Carroll",42),
				new Person("Thomas","Carlyle",51),
				new Person("Charlotte","Bronte",45),
				new Person("Matthew","Arnold",39));
		
		//Step 1: Sort list by last name
		Collections.sort(people, (p1,p2)->p1.getLastName().compareTo(p2.getLastName()));
		
		//Step 2: Create a method that prints all elements in the list
		printConditionally(people, p->true);
		
		//Step 3: Create a method that prints all people that have lastName beginning with C
		printConditionally(people, p->p.getLastName().startsWith("C"));
		
		printConditionally(people, p->p.getFirstName().startsWith("C"));
	}
	
	private static void printConditionally(List<Person> people, Predicate<Person> predicate) {
		for(Person p: people){
			if(predicate.test(p)){
				System.out.println(p);
			}
		}
	}
}

Lambda expression does not care about the interface as long as signature of the abstract method matches the signature of the lambda expression. So, there are some out of the box interfaces that come with JAVA in java.util.function package to handle some common scenarios. Previously we saw that first step to creating a lambda expression is to create an interface for it & define a method in that interface which has the same signature as the lambda expression.

So now if you find an interface in java.util.function package which pretty much addresses the lambda expression which you are writing then you don’t have to create the interface as well. You can use the out of the box interfaces in java.util.function package. Now, we have completely bypassed the need of creating the interface & we just have to find the correct interface in java.util.function package & we can just use that.

About Anoop Kumar Rai