Using Lambda expressions with JSF 2.2

JSF 2.2 comes in conjunction with EL 3.0. This version of the expression language offers a rarely known highlight: It is capable of using lambda expressions and streams. Even thought defined withing the Jave EE 7 spec (and not Java 8), you can still use these cool features!

Now, let’s build up a simple example:

A car dealer offers a bunch of cars. These cars will be listed on his web site.

Here are the simplified classes. Don’t care about the sense – it’s just for demo purpose. (Package and imports are omitted for brevity.)

public class Car {
    private final String _name;
    private final String _color;

    public Car(String name, String color) {
        _name = name;
        _color = color;

    public String getName() {
        return _name;

    public String getColor() {
        return _color;
public class Dealer {

    private final String _name;
    private final List<Car> _cars = new ArrayList<>();

    public Dealer(String name) {
        _name = name;

    public String getName() {
        return _name;

    public List<Car> getCars() {
        return _cars;

These classes are populated to JSF by a simple bean:

public class DemoBean {

    private final Dealer _dealer = new Dealer("NoName");

    public DemoBean() {

    public Dealer getDealer() {
        return _dealer;

    private void initCars() {
        _dealer.getCars().add(new Car("VW", "red"));
        _dealer.getCars().add(new Car("Ford", "green"));
        _dealer.getCars().add(new Car("Chevrolet", "red"));
        _dealer.getCars().add(new Car("BMW", "blue"));
        _dealer.getCars().add(new Car("Ferrari", "red"));
        _dealer.getCars().add(new Car("Mercedes", "green"));
        _dealer.getCars().add(new Car("Mercedes", "blue"));

Last, but not least, all cars of this dealer are displayed in a h:dataTable:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns=""
        <title>Lambda demo</title>
        <h1>Lambda demo</h1>
        <h:dataTable value="#{}" var="car">
                <f:facet name="header">
                    <h:outputLabel value="Trademark"/>
                <h:outputText value="#{}"/>
                <f:facet name="header">
                    <h:outputLabel value="Color"/>
                <h:outputText value="#{car.color}"/>

In this approach, all cars are displayed. Now, we want to display the blue cars only. A traditional approach is to modify the backing bean: Add a getter which offers a list of blue cars. But, using lambdas, there is a more elegant way. This feature enables us to add a filter directly to the web page.

Simply replace

<h:dataTable value="#{}" var="car">


<h:dataTable value="#{>car.color.equals('blue')).toList()}" var="car">

What happened?

Our list of cars is transformed into a stream. We apply a filter to this stream using a lambda expression (indicated by “->”). This means: For every element of the stream (which are cars), perform a boolean test. If true, take that element. Finally, a list is created by all elements according to that filter.

That’s it.

In a real world scenario, you would not compare to a hard-coded string, but variable content. For example, let the user choose his favorite color.

If you develop this app using NetBeans, you may try the version without lambdas first. NetBeans offers you a perfect autocomplete for the variable “car”. Once, you are ready, you add the stream. Using the filtered list, NetBeans is not able to offer autocomplete for the looping var. I’m shure, this get enhanced some day… 😉






4 thoughts on “Using Lambda expressions with JSF 2.2”

  1. Hi,

    I am using lambda expression in my JSF page but I am getting this error.

    Failed to parse the expression [#{>‘Glass’))..toList()}]

    I am using Netbeans 8.0.2 and Maven Web Application project. Server library = JSF 2.1 and Registered Libraries = JSF 2.2 showing up in the project properties.


    1. Hi Umar,

      Is this really your code, or just a typo?
      Watch out the two dots.


Leave a Reply

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