In Java 8 I can map over streams with the map method, e.g.
Stream.of("Hello", "world").map(s -> s.length())
gives me a stream containing the integers [5, 5]. I am trying to do the same with lists. I have come up with
List<String> list = ...
list.stream().map(s -> s.length()).collect(Collectors.toList())
This works but is rather verbose. Is there a more concise solution? Ideally, there would be a similar map method for lists, but I haven't found any. So, are there any alternatives?
list.stream().collect(Collectors.mapping(String::length, Collectors.toList())), but how compact do you want it to be?mapmethod, or (2) using streams instead of lists. But I am using Hibernate and when a class has a one-to-many association, it returns a collection of associated objects as a list, not as a stream. If I want to map on that collection (which happens extremely often), I have to convert between list and stream all the time.