迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 中的箭头运算符 ->

作者:迹忆客 最近更新:2023/11/13 浏览次数:

本文介绍了箭头运算符 (->) 在 Java 中的作用,并列出了一些示例代码来理解该主题。

在 Java 8 中,增加了一个新特性 lambda 表达式,同时在 Java 中出现了箭头运算符,用于形成 lambda 表达式。它将参数与表达式主体分开。

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

Lambda 表达式是在 Java 8 中引入的,可以用来代替 Java 中的匿名类,使代码更加简洁和可读。

下面是我们如何在 Java 8 之前的 Java 中创建匿名类的示例。

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

这就是我们如何在 Java 8 中使用 lambda 表达式来实现上述任务。

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

让我们从一些示例开始,以了解 lambda 和箭头运算符的使用。


Java 中箭头运算符的使用

在此示例中,我们使用箭头运算符创建了一个 lambda 表达式,该表达式实现了 Drawable 接口的 draw() 方法。请参考下面的示例。

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

输出:

Drawing width:  20

由于 lambda 表达式是使用函数式方法创建简洁代码的一种超级方式,因此我们可以在 Java 代码中以多种方式使用它。让我们看一些我们可以应用它的例子。


Java 集合中的箭头运算符

在此示例中,我们使用 lambda 表达式过滤 ArrayList 数据。我们使用了流 API 和 filter() 方法来获得所需的结果。你会注意到,使用 lambda 创建代码比使用非 lambda 代码更容易。请参考下面的示例。

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

输出:

Iphone 12: 100500.0
Sony Xperia: 25000.0

Java 线程中的箭头运算符

这是 lambda 表达式的另一种用法,我们用它来实现可运行接口的 run() 方法。由于 Runnable 是单个方法接口,因此很容易使用 lambda 表达式。请参考下面的示例。

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

输出:

Thread is running...

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

在 Pandas 的列中展平层次索引

发布时间:2024/04/24 浏览次数:1782 分类:Python

在这篇文章中,我们将使用不同的函数来使用 Pandas DataFrame 列来展平层次索引。我们将使用的方法是重置索引和 as_index() 函数。

Pandas 中的 Groupby 索引列

发布时间:2024/04/23 浏览次数:89 分类:Python

本教程将介绍如何使用 Python Pandas Groupby 对数据进行分类,然后将函数应用于类别。通过示例使用 groupby() 函数按 Pandas 中的多个索引列进行分组。

Pandas 中的散点矩阵

发布时间:2024/04/23 浏览次数:125 分类:Python

本教程演示了如何使用 scatter_matrix 函数在 Pandas 中创建散点图。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便