Java Language Streams Collect Results of a Stream into an Array


Analog to get a collection for a Stream by collect() an array can be obtained by the Stream.toArray() method:

List<String> fruits = Arrays.asList("apple", "banana", "pear", "kiwi", "orange");

String[] filteredFruits =
    .filter(s -> s.contains("a"))

// prints: [apple, banana, pear, orange]

String[]::new is a special kind of method reference: a constructor reference.