Is there any way to convert a 2D List to 1D List using only `map` not using `flatMap`?

你。 提交于 2020-12-30 08:34:11

问题


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

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]

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

Are there any way to make the code with same logic, just using map, not using 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.


回答1:


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);

Output is the same as yours:

[1, 2, 3, 4]

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

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

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.




回答2:


You can remove the flatMap if you perform the flat-mapping during collect:

List<Integer> lst1 = list_2d
    .stream()
    .collect(Collectors.flatMapping (List::stream, Collectors.toList()));

However, I don't see the advantage of using Collectors.flatMapping instead of flatMap.




回答3:


May be this is not what you are looking for, but if the goal is to add all sublists to a one resulting list, using for each might also be an option in terms of simple and basic:

List<List<Integer>> list_2d = List.of(List.of(1, 2), List.of(3, 4));
List<Integer> result = new ArrayList<>();
list_2d.forEach(list -> result.addAll(list));
System.out.println(result);



回答4:


As there are already several answers providing different implementations, I will try to show the difference between map and flatMap.

The map operation is used for changing every element of a stream to exactly one other element. In your example, possible use-cases could be either doing some aggregation on the list (e. g. summing all elements), yielding a List<Integer> containing all the sums of the individual lists. Or you could change the data structure, maybe to a set, yielding a List<Set<Integer>>. The number of elements in your resulting list, will always be 2 as the original list had 2 elements.

The flatMap operation on the other hand is used to convert one element to zero or more elements. Consider a class Family consisting of a number of Humans. If you get a list of Families from somewhere, but you just care about the individual people, you can flatMap the Family list to a list of Humans.

The difference between flatMap and the implementations given in the other answers is the type of operation: flatMap is an intermediate operation, meaning the result is a Stream. collect and reduce are terminal operations, meaning the result is something different (in your example: a List<Integer>. If you want to perform more stream-operations (e. g. filter or another map) you would have to create another Stream of the resulting list.

To illustrate this, consider having the list given in your question and as a result you want to have all even numbers from the list. I am using the collect solution from this answer. As you can see, using flatMap saves an unnecessary creation of a List as well as some lines of code.

public static void usingFlatMap() {
    List<List<Integer>> originalList = Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3, 4));

    List<Integer> result = originalList
            .stream()
            .flatMap(list -> list.stream())
            .filter(number -> number % 2 == 0)
            .collect(Collectors.toList());

    System.out.println(result); // [2, 4]
}

public static void usingCollect() {
    List<List<Integer>> originalList = Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3, 4));

    List<Integer> intermediateList = originalList
            .stream()
            .collect(Collectors.flatMapping(List::stream, Collectors.toList()));
    List<Integer> result = intermediateList
            .stream()
            .filter(number -> number % 2 == 0)
            .collect(Collectors.toList());

    System.out.println(result); // [2, 4]
}



回答5:


You can iterate over this 2d-list and add elements (integers) from it to another 1d-list:


  1. List<List<Integer>> list_2d = List.of(List.of(1, 2), List.of(3, 4));
    
    List<Integer> list_1d = new ArrayList<>();
    
    IntStream.range(0, list_2d.size()).forEach(i ->
            IntStream.range(0, list_2d.get(i).size()).forEach(j ->
                    list_1d.add(list_2d.get(i).get(j))));
    
    System.out.println(list_1d); // [1, 2, 3, 4]
    

  1. List<List<Integer>> list_2d = List.of(List.of(1, 2), List.of(3, 4));
    
    List<Integer> list_1d = new ArrayList<>();
    
    list_2d.forEach(list ->
            list.forEach(element ->
                    list_1d.add(element)));
    
    System.out.println(list_1d); // [1, 2, 3, 4]
    


来源:https://stackoverflow.com/questions/65378828/is-there-any-way-to-convert-a-2d-list-to-1d-list-using-only-map-not-using-fla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!