在包含Streams的2D列表的修改列表中包含未修改列表中的元素 [英] Including elements from unmodified lists in modified lists in a 2D list with Streams

查看:255
本文介绍了在包含Streams的2D列表的修改列表中包含未修改列表中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了这个关于使用1D列表做同样事情的问题,认为将答案应用于2D列表很容易,但事实并非如此。

I asked this question about doing the same thing with 1D lists, figuring that it would be easy to apply the answers to 2D lists, but it was not.

基于我的代码 Holger的回答,我提出了这个解决方案:

Basing my code off of Holger's answer, I came up with this solution:

//list is of type ArrayList<List<Integer>>
list.stream()
        .map(l -> Stream.concat(
                l.subList(0, l.size() - 1).stream().map(i -> i - 2),
                Stream.of(l.get(l.size() - 1)).collect(Collectors.toList())
        ))
        .collect(Collectors.toList());

我得到编译时错误无法推断类型参数< R> map(函数<?super T,?extends R>),但是,当我尝试这个时。

I get the compile-time error Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>), however, when I try this.

我怎样才能执行此功能在2D列表上?

How is can I perform this function on a 2D list?

一些示例输入和输出:

[[1 ,2,3,4],[5,6,7,8]] 应输出 [[ - 1,0,1,4],[3,4], 5,8]]

推荐答案

您的括号有问题。更改此:

You have a problem with the parenthesis. Change this:

list.stream()
    .map(l -> Stream.concat(
            l.subList(0, l.size() - 1).stream().map(i -> i - 2),
            Stream.of(l.get(l.size() - 1)).collect(Collectors.toList())
    ))
    .collect(Collectors.toList());

对此:

list.stream()
    .map(l -> Stream.concat(
            l.subList(0, l.size() - 1).stream().map(i -> i - 2),
            Stream.of(l.get(l.size() - 1))).collect(Collectors.toList())
    )
    .collect(Collectors.toList());

缩进在这里很重要,可以提高代码的可读性。我会重新启动第二个片段如下:

Indentation is important here, to improve code readability. I would reindent the second snippet as follows:

list.stream()
    .map(l -> Stream.concat(
            l.subList(0, l.size() - 1).stream().map(i -> i - 2),
            Stream.of(l.get(l.size() - 1)))
        .collect(Collectors.toList()))
    .collect(Collectors.toList());

或者更好,我会将修改内部列表的逻辑移动到辅助方法:

Or even better, I would move the logic that modifies the inner lists to a helper method:

public final class Utility {
    private Utility() { }

    public static List<Integer> modifyAllButLast(List<Integer> list) {
        return Stream.concat(
                l.subList(0, l.size() - 1).stream().map(i -> i - 2),
                Stream.of(l.get(l.size() - 1)))
            .collect(Collectors.toList());
    }
}

然后我会从外面使用这种方法stream:

And then I would just use this method from the outer stream:

list.stream()
    .map(Utility::modifyAllButLast)
    .collect(Collectors.toList());

注意:无需将辅助方法移动到新的 Utility class,你甚至可以让它成为非静态方法。在这种情况下,只需相应地更改 Utility :: modifyAllButLast 方法引用。

Note: there's no need to move the helper method to a new Utility class and you might even let it be a non-static method. In this case, just change the Utility::modifyAllButLast method reference accordingly.

这篇关于在包含Streams的2D列表的修改列表中包含未修改列表中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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