1

So, in Clojure, I can just say something like this...

(into [] (map some-function some-collection))

And I get a new vector of my transformed data.

Is there some simple equivalent of into for Java 8 streams? For example, I don't see a constructor on ArrayList that takes a stream, nor do I see some sort of helper function in java.util.Collections, nor the stream interface.

Alex Miller
  • 69,183
  • 25
  • 122
  • 167
BillRobertson42
  • 12,602
  • 4
  • 40
  • 57

1 Answers1

1

You can do it using Collectors:

someCollection.stream()
              .map(someFunction)
              .collect(Collectors.toList());

You can do other cool stuff with Collectors, as explained in its javadoc:

 Map<Department, Integer> totalByDept =
     employees.stream()
              .collect(Collectors.groupingBy(Employee::getDepartment,
                                               Collectors.summingInt(Employee::getSalary)));
Andrea Bergia
  • 5,502
  • 1
  • 23
  • 38