据我所知,有3种Java的lambda表达式。在Lambda表达式(爪哇),如何使用没有参数的表达式?
(int x, int y) -> { return x + y; }
x -> x * x
() -> x
第三人似乎从来没有使用过。
可不可以给每个各3例范例(3的情况下额外的例子将是很好的)来说明它们的使用情况如何?请使它们尽可能简单(优选与list.stream(开始)....)
据我所知,有3种Java的lambda表达式。在Lambda表达式(爪哇),如何使用没有参数的表达式?
(int x, int y) -> { return x + y; }
x -> x * x
() -> x
第三人似乎从来没有使用过。
可不可以给每个各3例范例(3的情况下额外的例子将是很好的)来说明它们的使用情况如何?请使它们尽可能简单(优选与list.stream(开始)....)
前两个例子是从最后一个不同。函数中的变量(lambda表达式)引用其参数。
虽然在第三个示例中,x
引用了lambda表达式之外但在词法范围内的变量(可以是来自方法或实例变量的局部变量)。
实施例1(通常为料流降低),通过将到目前为止计算总和和下一个项目从列表中lambda函数计算的总和:
int sum = list.stream().reduce((int x, int y) -> x+y);
例2中,计算从元件的平方:
squares = list.stream().map((int x) -> x*x).collect(Collectors.toList());
例3,设置元素为默认值,如果它在列表中的空:
final int x = MY_DEFAULT_VALUE;
// lambda refers the the variable above to get the default
defaults = list.stream().map((Integer v) -> v != null ? v : x);
或者例如3更好是在地图原子的方法:你在哪里得到2个参数的方法和返回值
int x = MY_DEFAULT_VALUE;
// lambda refers the the variable above to get the default
map.computeIfAbsent(1, (Integer key) -> x);
// the same could be achieved by putIfAbsent() of course
// but typically you would use (Integer key) -> expensiveComputeOfValue(x)
// ...
// or quite common example with executor
public Future<Integer> computeAsync(final int value) {
// pass the callback which computes the result synchronously, to Executor.submit()
// the callback refers to parameter "value"
return executor.submit(() -> computeSync(value));
}
第一个表达式将被使用。
第二个表达式将用于
x -> x * x
你在哪里得到了一种方法1个参数和返回值。将使用第三个表达式
() -> x
() -> x
其中您获得方法的0个参数并返回一个值。
让我们的第三个。假如你有一个不带任何参数和返回值的接口。
static interface RandomMath {
public int random();
}
现在你想给这个接口实例化一下它的实现。通常,其将被波纹管完成: -
Random random = new Random();
RandomMath randomMath = new RandomMath() {
@Override
public int random() {
return random.nextInt();
}
};
使用的λ它将是这样的: -
Random random = new Random();
RandomMath randomMath =() -> random.nextInt(); //the third type.
同样,对于前两个它可用于其中需要两个和一个参数的方法和返回一个值。
static interface PlusMath {
public int plus(int a, int b);
}
PlusMath plusMath = (a, b) -> a + b;
static interface SquareMath {
public int square(int a);
}
SquareMath squareMath = a -> a * a;
之前通过下面的实施例打算,首先,注意,Lambda表达式可以针对任何SAM(也称为功能性)接口被写入(逸岸,Lambda表达式是用于替换冗长匿名一个语法糖类(使用单一方法)在Java中)。
一个抽象方法的接口或功能接口是只包含一个abstract
方法的接口),你可以看看here。如果你知道这个简单点,你可以编写(使用)任意数量的你自己的Functional接口,然后根据每个Functional接口方法编写不同的Lambda表达式。
下面的例子已被写入通过利用现有的JDK(1.8)功能接口,如Callable
,Function
,BiFunction
(像这些,有许多内置功能接口在JDK 1.8,大多数时候他们容易套装我们的要求)。
(1)(中间体X,int y)对实施例 - > {返回X + Y; }
//Below Lamda exp, takes 2 Input int Arguments and returns string
BiFunction<Integer, Integer, String> biFunction = (num1, num2) ->
"Sum Is:" +(num1 + num2);
System.out.println(biFunction.apply(100, 200));
(2)对于x实施例 - > X * X
//Below Lamda exp, takes string Input Argument and returns string
list.stream().map((String str1) ->
str1.substring(0, 1)).
forEach(System.out::println);
(3)实施例为() - > X
//Below Lambda exp, Input argument is void, returns String
Callable<String> callabl =() -> "My Callable";
ExecutorService service = Executors.newSingleThreadExecutor();
Future<String> future = service.submit(callable);
System.out.println(future.get());
第三个似乎从未使用过?我只是用在这里:http://stackoverflow.com/questions/40797960/chicken-egg-lifecycle – ajb
供应商 SUP =() - > 2; –
Kachna
这些不是lambda表达式的不同“种类”(并且您甚至没有详尽地涵盖选项)。第三个仅仅是第一个的特例,其中只有零个参数。第二种是特殊的语法形式,如果只有一个参数,并且您选择让编译器推断参数类型,则可以省略括号。你也可以选择让编译器推断所有的参数类型,在这种情况下,你会得到类似'(x,y,z) - > x + y + z'的东西。但他们都是一样的“善良”的东西。 –