269

While hunting through some code I came across the arrow operator, what exactly does it do? I thought Java did not have an arrow operator.

return (Collection<Car>) CollectionUtils.select(listOfCars, (arg0) -> {
        return Car.SEDAN == ((Car)arg0).getStyle();
});

Details: Java 6, Apache Commons Collection, IntelliJ 12

Update/Answer: It turns out that IntelliJ 12 supports Java 8, which supports lambdas, and is "folding" Predicates and displaying them as lambdas. Below is the "un-folded" code.

return (Collection<Car>) CollectionUtils.select(listOfCars, new Predicate() {
    public boolean evaluate(Object arg0) {
        return Car.SEDAN == ((Car)arg0).getStyle();
    }
});
1
  • 10
    Note that the exact scenario is IntelliJ rendering the existing code as a lambda expression to make it easier to read. The actual code is not a lambda expression. Dec 29, 2016 at 13:29

6 Answers 6

211

That's part of the syntax of the new lambda expressions, to be introduced in Java 8. There are a couple of online tutorials to get the hang of it, here's a link to one. Basically, the -> separates the parameters (left-side) from the implementation (right side).

The general syntax for using lambda expressions is

(Parameters) -> { Body } where the -> separates parameters and lambda expression body.

The parameters are enclosed in parentheses which is the same way as for methods and the lambda expression body is a block of code enclosed in braces.

3
  • 12
    The OP using JDK 6, so this is really just IntelliJ folding code as per @antonm answer below Mar 1, 2013 at 18:14
  • 7
    @SeanLandsman OP might be using Java 6, but in the question he states that he came across that code elsewhere. As to why IntelliJ isn't reporting an error, that's a mystery (perhaps there is a Java 7 version installed somewhere in OP's system). But that's a lambda expression's syntax in Java, there's no question about it. Mar 1, 2013 at 18:54
  • 1
    Do you happen to know the name of this token type?
    – clankill3r
    Aug 13, 2020 at 21:20
80

This one is useful as well when you want to implement a functional interface

Runnable r = ()-> System.out.print("Run method");

is equivalent to

Runnable r = new Runnable() {
        @Override
        public void run() {
            System.out.print("Run method");
        }
};
7
  • 5
    why is this feature provided?
    – Akaisteph7
    Jun 6, 2019 at 20:07
  • 6
    @Akaisteph7 when you do not want to define a method because you find that only one caller is ever going to call that method. In those cases it makes sense to NOT define the method in a separate place rather pass in the body of the method, read about anonymous method followed by lambda. Jun 5, 2020 at 19:42
  • 4
    How can i see that specifically run() method was overwritten ?
    – Pirks
    Oct 22, 2020 at 7:19
  • 8
    Runnable contains other methods for overwriting. How specifically run() was chosen?
    – Pirks
    Oct 25, 2020 at 8:40
  • 7
    @Pirks - one can only use that on Interfaces that has only 1 method,
    – Witold
    Apr 20, 2021 at 13:42
35

I believe, this arrow exists because of your IDE. IntelliJ IDEA does such thing with some code. This is called code folding. You can click at the arrow to expand it.

2
  • 41
    @DavidConrad No, it's not wrong. IntelliJ does indeed fold inline implementations of functional interfaces to look like lambdas.
    – balpha
    Mar 13, 2013 at 9:19
  • 13
    Look at the question: "Details: Java 6, Apache Commons Collection, IntelliJ 12" Lambda in java 6 ???
    – Anton-M
    Mar 14, 2013 at 10:34
15

It's a lambda expression.

It means that, from the listOfCars, arg0 is one of the items of that list. With that item he is going to do, hence the ->, whatever is inside of the brackets.

In this example, he's going to return a list of cars that fit the condition

Car.SEDAN == ((Car)arg0).getStyle();
7

New Operator for lambda expression added in java 8

Lambda expression is the short way of method writing.
It is indirectly used to implement functional interface

Primary Syntax : (parameters) -> { statements; }

There are some basic rules for effective lambda expressions writting which you should konw.

0

The arrow operator is used to create lambda expressions, linking/separating parameters with the lambda body. syntax: (parameters) -> {expression}; It is also an efficient way of implementing functional interfaces like onClickListeners in java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.