I expected to be able to use Stream::flatMap like this
public static List duplicate(String s) {
List l = new ArrayList
If you want to duplicate each object in the stream several times, you don't need to waste memory on this with additional ArrayList. There are several shorter and faster alternatives.
Generate new stream using Stream.generate, then limit it:
listOfStrings.stream()
.flatMap(str -> Stream.generate(() -> str).limit(2))
.collect(Collectors.toList());
Generate sequence of numbers via IntStream.range and map them to the same string:
listOfStrings.stream()
.flatMap(str -> IntStream.range(0, 2).mapToObj(i -> str))
.collect(Collectors.toList());
Use good old Collections.nCopies:
listOfStrings.stream()
.flatMap(str -> Collections.nCopies(2, str).stream())
.collect(Collectors.toList());
If you are sure that you will always duplicate exactly two times, there's the shortest alternative:
listOfStrings.stream()
.flatMap(str -> Stream.of(str, str))
.collect(Collectors.toList());