有什么方法可以仅使用"map"而不使用"flatMap"将2D列表转换为1D列表吗? [英] Is there any way to convert a 2D List to 1D List using only `map` not using `flatMap`?

查看:66
本文介绍了有什么方法可以仅使用"map"而不使用"flatMap"将2D列表转换为1D列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java初学者,我刚刚了解了mapflatMap.

I'm a Java beginner, I just got learn about map and flatMap.

将2d列表转换为1d列表时,其实现方式如下.

When 2d List should be converted to 1d List, it was implemented like below.

List<List<Integer>> list_2d = List.of(List.of(1, 2), List.of(3, 4));

List<Integer> lst1 = list_2d
    .stream()
    .flatMap(arr -> arr.stream())
    .collect(Collectors.toList());
printAll(lst1); // [1, 2, 3, 4]

但是,我认为它可以不使用flatMap来实现.

But, I think it looks that it can be implemented not using flatMap.

有什么方法可以使代码具有相同的逻辑,仅使用map而不使用flatMap?

Are there any way to make the code with same logic, just using map, not using flatMap?

只问是因为map是否可以代替flatMap的全部,所以没有理由记住flatMap.我一直追求简单而基本的东西.

Just asking because if map can replace all of flatMap, there is no reason to memorize flatMap. I always pursue simple and basic things.

推荐答案

要回答所提出的问题,还有其他方法,但是我不建议我想到的任何方法.例如,您可以使用减少:

To answer the question as asked, there are other ways, but I wouldn’t recommend any that I can think of. For example you may use reduction:

    List<List<Integer>> list2d = List.of(List.of(1, 2), List.of(3, 4));

    List<Integer> lst1 = list2d
        .stream()
        .reduce((l1, l2) -> {
            ArrayList<Integer> concatenated = new ArrayList<>(l1);
            concatenated.addAll(l2);
            return concatenated;
        })
        .orElse(List.of()); // or else empty list
    
    System.out.println(lst1);

输出与您的输出相同:

[1、2、3、4]

[1, 2, 3, 4]

但是您的代码比我的代码容易理解.我建议你坚持下去.

But your code is much easier to understand than mine. I suggest you stick to it.

只是因为,如果地图可以替换所有flatMap,则没有理由 记住flatMap.我一直追求简单而基本的东西.

Just because, if map can replaced all of flatMap, there are no reason to memorize flatMap. I always pursue simple and basic thing.

您已经拥有了最简单,最基本的东西.另外,为了充分利用流的潜能,您将不时需要使用许多方法调用.在使用流多年之后,我有时仍会发现自己在Javadoc中查找它们.我必须查找reduce()的详细信息才能获得此答案.不要指望一切.不过,我确实很了解flatMap(),因为它通常很实用.

You already got the simplest and most basic thing there is. Also to get the full potential from streams, there are many method calls that you will need to use now and then. After having used streams for years, I still find myself looking them up in the Javadoc sometimes. I had to look up the details of reduce() for this answer. Don’t expect to have everything in your head. I do know flatMap() by heart, though, since it is often practical.

仅出于学术目的:通常您不能将flatMap()替换为map().否则您一开始就应该使用map().但是另一种方法是:您始终可以将map()替换为flatMap().您只是不想.例如,如果我们有:

Only out of academic interest: Typically you can’t replace flatMap() with map(). Or you would have used map() from the outset. But the other way around: You can always replace map() with flatMap(). You just wouldn’t want to. For example, if we had:

    List<String> strings = List.of("short", "somewhat longer");
    List<Integer> lengths = strings.stream()
            .map(String::length)
            .collect(Collectors.toList());
    System.out.println(lengths);

[5,15]

[5, 15]

如果由于某种奇怪的原因我们只记得flatMap(),而不是map(),我们可以这样做:

If for some strange reason we could only remember flatMap(), not map(), we could do:

    List<Integer> lengths = strings.stream()
            .flatMap(s -> Stream.of(s.length()))
            .collect(Collectors.toList());

我认为很明显,带给我们的只是不必要的麻烦.最好至少记住map()flatMap()两者,以便在需要时可以查找它们.

I think it’s clear to see that all it brings us is needless complication, though. It’s better to remember both of map() and flatMap() at least well enough to be able to look them up when we need them.

这篇关于有什么方法可以仅使用"map"而不使用"flatMap"将2D列表转换为1D列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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