简体   繁体   English

Java 中的箭头运算符“->”有什么作用?

[英]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.我以为 Java 没有箭头操作符。

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

Details : Java 6, Apache Commons Collection, IntelliJ 12详细信息: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.更新/回答:事实证明,IntelliJ 12 支持 Java 8,它支持 lambda,并且正在“折叠”谓词并将它们显示为 lambda。 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();
    }
});

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.这是新 lambda 表达式语法的一部分,将在 Java 8 中引入。有几个在线教程可以掌握它,这里有一个链接 Basically, the -> separates the parameters (left-side) from the implementation (right side).基本上, ->将参数(左侧)与实现(右侧)分开。

The general syntax for using lambda expressions is使用 lambda 表达式的一般语法是

(Parameters) -> { Body } where the -> separates parameters and lambda expression body. (Parameters) -> { Body }其中->分隔参数和 lambda 表达式主体。

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.参数用括号括起来,这与方法相同,lambda 表达式主体是用大括号括起来的代码块。

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

I believe, this arrow exists because of your IDE.我相信,这个箭头的存在是因为你的 IDE。 IntelliJ IDEA does such thing with some code. IntelliJ IDEA 用一些代码来做这样的事情。 This is called code folding.这称为代码折叠。 You can click at the arrow to expand it.您可以单击箭头将其展开。

It's a lambda expression.这是一个 lambda 表达式。

It means that, from the listOfCars, arg0 is one of the items of that list.这意味着,在 listOfCars 中,arg0 是该列表的项目之一。 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();

New Operator for lambda expression added in java 8在 java 8 中添加了用于 lambda 表达式的新运算符

Lambda expression is the short way of method writing. Lambda 表达式是方法编写的简写方式。
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.您应该了解一些编写有效 lambda 表达式的基本规则

runOnUiThread() is used when you want to update your UI from a Non-UI Thread.当您想从非 UI 线程更新您的 UI 时,使用 runOnUiThread()。 For eg- If you want to update your UI from a background Thread.例如,如果您想从后台线程更新您的 UI。 You can also use Handler for the same thing.你也可以使用 Handler 来做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM