Java流API将lambda表达式存储为变量 [英] Java Stream API storing lambda expression as variable

查看:143
本文介绍了Java流API将lambda表达式存储为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个标题对我来说听起来很愚蠢,但必须至少有一些聪明的方法才能达到这样的效果,我不知道如何解释它。我需要使用sorted in stream API对数组进行排序。到目前为止,这是我的流:

This title sounds stupid even to me, but there must be at least somewhat clever way to achieve such effect and I don't know how else to explain it. I need to sort array using sorted in stream API. Here is my stream so far:

Arrays.stream(sequence.split(" "))
        .mapToInt(Integer::parseInt)
        .boxed()
        .sorted((a, b) -> a.compareTo(b))
        .forEach(a -> System.out.print(a + " "));

现在我有两种不同的类型 - 升序和降序,我需要使用的排序在用户输入中。所以我想要做的就是切换2个案例:升序和降序以及分别存储lambda表达式的变量:

Now I have two different sorts of course - ascending and descending and the sort I need to use is specified in the user input. So what I want to do is having something like switch with 2 cases: "ascending" and "descending" and a variable to store the lambda expression respectively:

switch(command) {
    case "ascending": var = a.compareTo(b);
    case "descending": var = b.compareTo(a);
}

然后我的排序看起来像:

Then I my sorted looks like:

 .sorted((a, b) -> var)

我在参加的python课程中得到了这个想法。在那里可以将对象存储在变量中,从而使变量可执行。我意识到这个lambda不是一个对象,而是一个表达式,但我要问的是有什么聪明的方法可以达到这样的结果,或者我应该只有

I got the idea in a python course I attended. There it was available to store an object in variable, thus making the variable "executable". I realize that this lambda is not an object, but an expression, but I'm asking is there any clever way that can achieve such result, or should I just have

if(var)

以及每个排序顺序的两个不同的流。

and two diferent streams for each sort order.

推荐答案

这个问题根本不是愚蠢的。从更广泛的意义上回答:不幸的是,没有通用的解决方案。这是由于类型推断,它根据目标类型确定lambda表达式的一个特定类型。 (有关类型推断的部分可能会对此有所帮助,但不包括有关lambdas的所有细节。)

The question is not stupid at all. Answering it in a broader sense: Unfortunately, there is no generic solution for that. This is due to the type inference, which determines one particular type for the lambda expression, based on the target type. (The section about type inference may be helpful here, but does not cover all details regarding lambdas).

特别是,像 x这样的lambda - > y 没有任何类型。所以无法写作

Particularly, a lambda like x -> y does not have any type. So there is no way of writing

GenericLambdaType function = x - > y;

以及后来使用函数作为实际的替代品lambda x - > y

and later use function as a drop-in replacement for the actual lambda x -> y.

例如,当你有两个函数,比如

For example, when you have two functions like

static void useF(Function<Integer, Boolean> f) { ... }
static void useP(Predicate<Integer> p) { ... }

您可以使用相同的lambda调用它们

you can call them both with the same lambda

useF(x -> true);
useP(x -> true);

但无法存储 x - > true lambda在某种程度上可以传递给两个函数 - 你只能将它存储在一个带有所需类型的引用中以后:

but there is no way of "storing" the x -> true lambda in a way so that it later may be passed to both functions - you can only store it in a reference with the type that it will be needed in later:

Function<Integer, Boolean> f = x -> true;
Predicate<Integer>         p = x -> true;
useF(f);
useP(p);






对于您的特定情况,answer 已经展示了解决方案:您必须将其存储为比较器< Integer> (忽略你首先不需要lambda的事实......)


For your particular case, the answer by Konstantin Yovkov already showed the solution: You have to store it as a Comparator<Integer> (ignoring the fact that you wouldn't have needed a lambda here in the first place...)

这篇关于Java流API将lambda表达式存储为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆