Projecting and Filtering collections in SpEL

In the previous post under Spring SpEL, I showed how to apply filtering and then projection on a collection in SpEL.

In this post I will show with example how to apply projection and then filtering on a collection in SpEL.

For our example I will use the below Pojo class

Employee class

package spel.package16;

public class Employee {
    private int id;
    private String name;
    private int salary;

    public Employee(int id, String name, int salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    //Removing getter and setter for brevity
}

Below is the main class for your reference.

Main class

package spel.package16;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
@ComponentScan(basePackages = "spel.package16")
public class Example16 {
    @Value("#{employees.![name].?[length() == 6]}")
    private List filteredEmployeeNames;

    @Bean("employees")
    public List getEmployees() {
        List list = new ArrayList<>(0);
        for(int i = 1; i <= 10; i++) {
            Employee employee = new Employee(i, "name" + i, (i * 100));
            list.add(employee);
        }

        return list;
    }

    public static void main(String [] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example16.class);
        Example16 example16 = applicationContext.getBean(Example16.class);
        for(String name : example16.filteredEmployeeNames) {
            System.out.println(name);
        }
    }
}

In the above I create a bean named “employees” using “getEmployees” method.

The bean is list containing 10 employees.

In the above code at line 16, I created an SpEL expression, the result of the expression is a collection which is assigned to the instance variable “filteredEmployeeNames”.

In the expression first I apply projection on employees collection to get only the names of the employee. Refer to condition starting with “.!” and ending before “.?”.

The result of the projection (which is a collection of names) is next applied to the filter expression (refer to condition starting with “.?”)

The filter expression will filter out names whose length is not equal to 6.

The final result will be a collection of names whose length is equal to 6.

Please note, in this example, the filter condition is applied on collection containing data of type “String” i.e., name and not on collection containing data of type “Employee”.

In this way we can first apply projection and then filtering on a collection.

Below is the output

Output

name10

Leave a comment