Is there an utility method in Java that generates a list or array of a specified length with all elements equal to a specified value (e.g [\"foo\", \"foo\", \"foo\", \"foo\"
Using IntStream
, you can generate a range of integers, map them to the element you want and collect it as a list.
List list = IntStream.rangeClosed(0, 5)
.mapToObj(i -> "foo")
.collect(Collectors.toList());
Or, as an array
String[] arr = IntStream.rangeClosed(0, 5)
.mapToObj(i -> "foo")
.toArray(String[]::new);