Java 8: Simple Example of First-class Functions

A first-class object (or first-class citizen) in programming languages can be defined as an entity that supports the following operations: being passed as a parameter, being assigned to a variable and being returned from a function. In other words, everything that contains a value can be considered a first-class citizen.

In the previous versions of Java we had the following elements as first-class objects:

Primitive values: int, double, long, short, byte, char, etc.
Objects: new Integer(1), new ArrayList(), new Person(), etc.

Methods and classes, since they’re not values, were considered second-class.

Now with Java 8, functional-style programming was introduced to the “Java World”, methods can now be treated as values, which means that now, like in Scala and JavaScript, they’re also first-class citizens and they can be passed as parameters, assigned to variables and also returned from functions.

Here is a simple example of how to use this Java 8 feature:

Let’s suppose we have a list of objects and we want to filter it by some specific attribute, this is the conventional way to do it:

1
2
3
4
5
6
7
8
9
public List filterPersonByAge(List<Person> list) {
        List result = new ArrayList();
        for (Person person : list) {
            if(p.age > 65){
                result.add(person);
            }
        }
        return result;
}

In this case I’m just filtering and returning everyone with age greater than 65, the problem is that if I need to filter this list again by another attribute I’ll have to duplicate this code and modify just one single line. That’s exactly the advantage of Java 8 first-class functions, by using them we don’t have to duplicate the code.

Here is the same method rewritten using first-class functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.function.Predicate;
 
public class FirstClassFunctionExample {
 
   public List filterPerson(List<Person> list, Predicate<Person> p) {
       List result = new ArrayList();
       for (Person person : list) {
           if(p.test(person)){
               result.add(person);
           }
       }
       return result;
   }
 
   public boolean ageFilter(Person p){
       return p.age > 65;
   }
}

Note that I’ve added a new parameter of the type Predicate, which will be the value that contains the condition I’m going to test in the line 8. I’ve also implemented the ageFilter method, which is the one I’m going to pass as the parameter ‘p’.

And finally, this is how we call the filterPerson method passing ageFilter as a parameter:

1
2
 
filterPerson(personList, FirstClassFunctionExample::ageFilter);

That’s it! Now if I need to filter by another attribute I just have to change a little bit the filter implementation (or write another filter), the method that will actually filter the list will be reused.

Source: Java 8 in Action

One comment on “Java 8: Simple Example of First-class Functions

  1. kaushali

    Thank you very much for this. I have read books about java 8 features, But I didn’t get clear idea about that. Now I got idea.

Leave a Reply

Your email address will not be published. Required fields are marked *

Obs: Use the tag <pre lang="LANGUAGE"> to include code blocks to your comment.
Example: <pre lang="javascript"> console.log('Test'); </pre>