The Arrow Operator -> in Java

Mohammad Irfan Oct 17, 2021 Sep 25, 2021
  1. How to Use Arrow Operator in Java
  2. Arrow Operator in Java Collections
  3. Arrow Operator in Java Thread
The Arrow Operator -> in Java

This tutorial introduces what the arrow operator (->) does in Java and also lists some example codes to understand the topic.

In Java 8, a new feature lambda expression was added, and at the same time, the arrow operator came into existence in Java which is used to form lambda expression. It separates the arguments from the expression body.

(parameters) -> { statements; } // Lambda expression having arrow

Lambda expression was introduced in Java 8 and can be used in place of anonymous class in Java to make code more concise and readable.

Below is an example of how we create an anonymous class in Java before Java 8.

Runnable r = new Runnable() {
        @Override
        public void run() {
            System.out.print("Run method");
        }
};

And this is how we can implement the above task using lambda expression in Java 8.

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

Let’s start with some examples to understand the use of lambda and arrow operators as well.

How to Use Arrow Operator in Java

In this example, we used the arrow operator to create a lambda expression that implements the draw() method of the Drawable interface. See the example below.

interface Drawable{
    public void draw();  
}  
public class Main {
    public static void main(String[] args) {
        int width = 20;  
        // arrow operator  
        Drawable d2=()->{
            System.out.println("Drawing width:  "+width);  
        };  
        d2.draw();  
    }
}

Output:

Drawing width:  20

Since lambda expression is a super way to create concise code with a functional approach, we can use it in several ways in the Java code. Let’s see some examples where we can apply it.

Arrow Operator in Java Collections

In this example, we filter ArrayList data by using a lambda expression. We used the stream API and filter() method to get the desired result. You can notice that how easy it is to create code using lambda than non-lambda code. See the example below.

package javaexample;
import java.util.ArrayList;  
import java.util.List;  
import java.util.stream.Stream;   
class Product{
    int id;  
    String name;  
    float price;  
    public Product(int id, String name, float price) {
        this.id = id;  
        this.name = name;  
        this.price = price;  
    }  
}  
public class Main{
    public static void main(String[] args) {
        List<Product> list=new ArrayList<Product>();  
        list.add(new Product(1,"Samsung S5",17000));  
        list.add(new Product(3,"Iphone 12",100500));  
        list.add(new Product(2,"Sony Xperia",25000));  
          
        // using arrow to filter data  
        Stream<Product> filtered_data = list.stream().filter(p -> p.price > 17000);
        filtered_data.forEach(  
                product -> System.out.println(product.name+": "+product.price)  
        );  
    }
}

Output:

Iphone 12: 100500.0
Sony Xperia: 25000.0

Arrow Operator in Java Thread

This is another use of lambda expression where we used it to implement the run() method of the runnable interface. Since Runnable is a single method interface, then it is easy to use a lambda expression. See the example below.

package javaexample;
import java.util.ArrayList;  
import java.util.List;  
import java.util.stream.Stream;   

public class Main{
    public static void main(String[] args) {

        // using arrow  
        Runnable r=()->{
            System.out.println("Thread is running...");  
        };  
        Thread t2 = new Thread(r);  
        t2.start();  
    }
}

Output:

Thread is running...

Related Article - Java Operator