Retrieving a List from a java.util.stream.Stream in Java 8
What you are doing may be the simplest way, provided your stream stays sequential—otherwise you will have to put a call to sequential() before forEach
.
[later edit: the reason the call to sequential() is necessary is that the code as it stands (forEach(targetLongList::add)
) would be racy if the stream was parallel. Even then, it will not achieve the effect intended, as forEach
is explicitly nondeterministic—even in a sequential stream the order of element processing is not guaranteed. You would have to use forEachOrdered
to ensure correct ordering. The intention of the Stream API designers is that you will use collector in this situation, as below.]
An alternative is
targetLongList = sourceLongList.stream()
.filter(l -> l > 100)
.collect(Collectors.toList());
java 8 streams copy specific objects from map to list
Figured out a possible solution myself:
return Stream.of(fruitNames)
.map(fruitMap::get)
.filter(Objects::nonNull)
.collect(Collectors.toList());
Get item from first list of list inside list - java 8
Conceptually this can be reframed as trying to get the "greatest" BankConversionFees
, where "greatest" means the one containing the single greatest individual rate.
This can be represented in a simple, straightforward manner using Stream.max(Comparator)
with a comparator that compares the BankConversionFees
based on the largest rate (treating any empty fee history lists as smaller than any non-emtpy ones):
Comparator<BankConversionFees> comparator = Comparator.comparing(
f -> f.getFeesHistory().stream().map(FeesHistory::getRate).max(Comparator.naturalOrder()),
Comparator.nullsFirst(Comparator.comparing(o -> o.orElse(null))));
return allFees.stream().max(comparator);
If you want to continue to return an empty optional in the case where no BankConversionFees
has any rates, as opposed to returning one of the fees without rates, you can filter accordingly:
Comparator<BankConversionFees> comparator = Comparator.comparing(
h -> h.getFeesHistory().stream().map(FeesHistory::getRate).max(Comparator.naturalOrder()).orElseThrow());
return allFees.stream().filter(f-> !f.getFeesHistory().isEmpty()).max(comparator);
How can I get a List from some class properties with Java 8 Stream?
You can use map
:
List<String> names =
personList.stream()
.map(Person::getName)
.collect(Collectors.toList());
EDIT :
In order to combine the Lists of friend names, you need to use flatMap
:
List<String> friendNames =
personList.stream()
.flatMap(e->e.getFriends().stream())
.collect(Collectors.toList());
Related Topics
Functional Style of Java 8's Optional.Ifpresent and If-Not-Present
Get Maven Artifact Version at Runtime
Maven Compilation Error: (Use -Source 7 or Higher to Enable Diamond Operator)
Should You Report the Message Text of Exceptions
Inserting Text into an Existing File via Java
Jformattedtextfield:Input Time Duration Value
How to Ensure Hashcode() Is Consistent with Equals()
How to Retrieve Image from Project Folder
How to Create a Standalone .Exe in Java (That Runs Without an Installer and a Jre)
Eclipse Windowbuilder, Overlapping JPAnels
Static Allocation in Java - Heap, Stack and Permanent Generation
How to Format Localdate Object to Mm/Dd/Yyyy and Have Format Persist
Javafx Automatic Resizing and Button Padding
How to Test to See If a Double Is Equal to Nan