0

What is the meaning of the java operator "->" like in the following code, taken from the initialization of a SpringBoot application:

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {

        return (container -> {
            ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
            ErrorPage error403Page = new ErrorPage(HttpStatus.FORBIDDEN, "/403.html");
            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
            ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

            container.addErrorPages(error401Page, error403Page, error404Page, error500Page);
        });
    }
DanielC
  • 357
  • 1
  • 4
  • 10

1 Answers1

2

This is for a lambda expression, a language feature first introduced in Java 8. Basically this is an inline anonymous function that takes container as a parameter. Usually lambdas return values but here it looks like it is just carrying out the "side effect" of calling addErrorPages to container. There is no type specified for container as Java intuits it from the context.

Lambda expressions are more than a language feature, they are also a whole area of computer science and functional programming. A good SO post describing them is here.

Community
  • 1
  • 1
sparc_spread
  • 10,643
  • 11
  • 45
  • 59