JIYIK CN >

Current Location:Home > Learning > PROGRAM > Java >

Arrow operator in Java ->

Author:JIYIK Last Updated:2025/04/14 Views:

This tutorial explains ->the role of the arrow operator ( ) in Java and lists some sample code to understand the topic.

In Java 8, a new feature lambda expression was added, and the arrow operator appeared in Java to form lambda expressions. It separates the parameters from the expression body.

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

Lambda expressions were introduced in Java 8 and can be used to replace anonymous classes in Java, making the code more concise and readable.

Below is an example of how we create anonymous classes in Java prior to Java 8.

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

This is how we can achieve the above task using lambda expressions 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.


Use of Arrow Operator in Java

In this example, we have used the arrow operator to create a lambda expression that implements the method Drawableof the interface draw(). Refer to the following example.

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 expressions are a super way to create concise code using a functional approach, we can use it in many ways in our Java code. Let's look at some examples where we can apply it.


Arrow operator in Java collections

In this example, we use lambda expression to filter ArrayList data. We have used stream API and filter() method to get the required result. You will notice that it is easier to create code using lambda than non-lambda code. Refer to the following example.

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 threads

This is another use of lambda expression, where we use it to implement run()a method of the Runnable interface. Since Runnable is a single method interface, it is easy to use lambda expression. Refer to the following example.

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...

Previous:>> operator in Java

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

>> operator in Java

Publish Date:2025/04/14 Views:66 Category:Java

This guide will introduce you to the operator in Java . To understand this concept, you need to be familiar with some lower-level computing concepts. For example, bits, bytes, etc. Let's take a deeper look. Operators in Java In Java, the op

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Sending Emails Using the Mail Form in PHP

Publish Date:2025/04/12 Views:116 Category:PHP

This article will demonstrate installing sendmail the library and sending an email through the PHP mail form. Install sendmail to send email from PHP on local server PHP has a built-in function mail() to send emails. However, this function

Plotting a Pandas Series

Publish Date:2025/04/13 Views:102 Category:Python

This article explores the concept of plotting series on a DataFrame using Pandas. Whether you are exploring a dataset to hone your skills or aiming to make a good presentation for your company’s performance analysis, visualization plays a

GroupBy Application in Pandas

Publish Date:2025/04/13 Views:177 Category:Python

This tutorial aims to explore the concepts in Pandas GroupBy Apply . Pandas is used as an advanced data analysis tool or package extension in Python. When we have data in SQL tables, spreadsheets, or heterogeneous columns, Pandas is highly

Flattening Hierarchical Indexes in Pandas Columns

Publish Date:2025/04/12 Views:183 Category:Python

This article will discuss how to flatten a hierarchical index in a Pandas Dataframe column. Groupby aggregation functions are often used to create hierarchical indexes. The used aggregation function will be visible in the hierarchical index

DATETIME vs. TIMESTAMP in MySQL

Publish Date:2025/04/11 Views:117 Category:MySQL

DATETIME and TIMESTAMP are two different data types that can be used to store values ​​that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea

Execute multiple joins in one query in MYSQL

Publish Date:2025/04/11 Views:94 Category:MySQL

Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.

Copying rows in a MySQL database

Publish Date:2025/04/11 Views:182 Category:MySQL

Today's topic is about duplicating rows in MySQL database. We will learn how to copy a row and paste it into the same table with auto increment ID and custom ID. We will also see how to copy multiple fields from multiple rows of one table a

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial