How can I get the Left part of a MutablePair list with Java 8 - streams, forEach or something? The code I want to transform in Java 8 type is:
for (MutablePair<String, Long> id: list) {
if (null == value) {
value = id.getLeft();
} else {
value += ", " + id.getLeft();
}
}
So, if I have the list as: [(OA,3853), (EE,866), (UN,728), (PP,10)], I need the value as: OA, EE, UN, PP as simple as possible. Many Thanks.
use map intermediate operation to transform to a Stream<String> then the joining collector to accumulate the elements into a single string separated by a ", " delimiter:
list.stream()
.map(pair -> pair.getLeft()) // or MutablePair::getLeft
.collect(Collectors.joining(", "));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With