D
D
dc65k2020-10-17 09:56:08
Java
dc65k, 2020-10-17 09:56:08

How to properly make a concat List?

Hello everyone, can you tell me how correct this approach is for concatenating lists using the Stream API concat?

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test {
    public static void main(String[] args) {

        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        List<Integer> list3;

        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);
        list1.add(5);
        list2.add(6);
        list2.add(7);
        list2.add(8);
        list2.add(9);
        list2.add(10);

        System.out.println(list1);
        System.out.println(list2);

        Stream<Integer> stream1 = Stream.concat(list1.stream(), list2.stream());
        System.out.println(stream1);

        list3 = (List<Integer>) stream1.collect(Collectors.toList());

        System.out.println(list3);

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2020-10-17
@dc65k

Just uselist.addAll

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question