什么 - > < - 操作员吗? [英] What does the -> <- operator do?
问题描述
我最近发现了以下代码:
I recently came upon the following code:
IntPredicate neg = x -> x <- x;
这是什么,某种反向双lambda?
What is this, some sort of reverse double lambda?
推荐答案
没有 - > < -
运算符。第一个 - >
只是lambda语法,如Java 8中所介绍的那样,第二个< -
是一个'小于'<
和'一元减去' -
的误导性连接。
There is no -> <-
operator. That first ->
is just lambda syntax, as introduced in Java 8, and that second <-
is a misleading concatenation of 'smaller than' <
and 'unary minus' -
.
您可以将其读作 IntPredicate neg =(x) - > (x <( - x));
,即它测试 x
是否小于 -x
,这是所有人的情况(好吧,大多数)负数,因此名称 neg
。
You can read it as IntPredicate neg = (x) -> (x < (-x));
, i.e. it tests whether x
is smaller than -x
, which is the case for all (well, most) negative numbers, hence the name neg
.
IntPredicate neg = x -> x <- x;
System.out.println(neg.test(4)); // false
System.out.println(neg.test(0)); // false
System.out.println(neg.test(-4)); // true
仅为完整性:此测试是不仅(故意?)难以理解,但是 - 正如评论中所指出的那样 - 它也失败了 Integer.MIN_VALUE
(这是 == - Integer.MIN_VALUE的
)。相反,你应该只使用更简单的 IntPredicate neg = x - > (x <0);
。
Just for completeness: This test is not only (intentionally?) hard to understand, but -- as pointed out in the comments -- it also fails for Integer.MIN_VALUE
(which is ==-Integer.MIN_VALUE
). Instead, you should probably just use the much simpler IntPredicate neg = x -> (x < 0);
.
这篇关于什么 - > < - 操作员吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!