Rylander
Rylander

Reputation: 20129

What does the arrow operator, '->', do in Java?

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();
    }
});

Upvotes: 279

Views: 459332

Answers (7)

Andreas Panagiotidis
Andreas Panagiotidis

Reputation: 3002

Just an example that works

interface StringFunction {  
  String run(String strS, String strT);
}

public class Main {
  public static void main(String[] args) {  
    
    //Implement a function 
    StringFunction concatWithDotsFunction = (s , t) -> s + "..." + t;
    
    String concatWithDotsResult = concatWithDotsFunction.run("Hello" , "you");
    System.out.println("concatWithDotsResult: " + concatWithDotsResult);
        
  
    //Implement a function 
    StringFunction concatWithUnderscoreFunction = (s , t) -> s + " _ " + t;
    
    //Call a method that takes the function as argument
    printFormatted("Hello", "you" , concatWithUnderscoreFunction);
        
  }
  
  
  public static void printFormatted(String strS, String strT, StringFunction function) {
    String result = function.run(strS, strT);
    
    System.out.println("result: " + result);    
  }
  
}

Upvotes: 1

hiro hamanda
hiro hamanda

Reputation: 11

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.

Upvotes: 1

Justin Patel
Justin Patel

Reputation: 1433

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");
        }
};

Upvotes: 84

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236004

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.

Upvotes: 220

Manasi
Manasi

Reputation: 765

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.

Upvotes: 7

Anton-M
Anton-M

Reputation: 1111

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.

Upvotes: 35

simple-thomas
simple-thomas

Reputation: 729

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();

Upvotes: 15

Related Questions