How do I join two lists in Java?

后端 未结 30 1684
旧巷少年郎
旧巷少年郎 2020-11-22 14:36

Conditions: do not modifiy the original lists; JDK only, no external libraries. Bonus points for a one-liner or a JDK 1.3 version.

Is there a simpler way than:

30条回答
  •  囚心锁ツ
    2020-11-22 15:18

    You can create your generic Java 8 utility method to concat any number of lists.

    @SafeVarargs
    public static  List concat(List... lists) {
        return Stream.of(lists).flatMap(List::stream).collect(Collectors.toList());
    }
    

提交回复
热议问题