This question already has answers here:
How can I turn a List of Lists into a List in Java 8?
(12 answers)
Closed 5 years ago.
I am trying to concat list of a stream and process it.
class A {
public List<B> bList;
}
List<A> aList;
aList.stream().map(a -> a.bList)....
Here i get several list of b.
But, I would like to collect all my b in only one list. Any ideas ?
That's what flatMap is for :
List<B> bList = aList.stream()
.flatMap(a -> a.bList.stream())
.collect(Collectors.toList());
Related
This question already has answers here:
Every combination of 2 strings in List Java 8 [duplicate]
(1 answer)
Should I use Java 8 Streams Api to combine two Collections?
(2 answers)
Closed 2 years ago.
I have two Lists:
List<Object1> listOne = provider.getObjects().stream().collect(Collectors.toList());
List<Object2> listTwo = provider2.getObjects().stream().collect(Collectors.toList());
Now I want create List containg all possible Object1-Object2 combinations: List<ObjectCombinations> result;
class ObjectCombinations {
Object1 object1;
Object2 object2;
public ObjectCombinations(Object1 object1, Object2 object2) {
this.object1 = object1;
this.object2 = object2;
}
}
How is that possible with java 8 streams?
You can use flatMap to get all the combinations:
List<ObjectCombinations> result =
listOne.stream()
.flatMap(o1 -> listTwo.stream()
.map(o2 -> new ObjectCombinations(o1,o2)))
.collect(Collectors.toList());
First you create a Stream<Object1>, then you use flatMap to combine each element of that stream with all the elements of listTwo, and create the ObjectCombinations instances, which you collect to a List.
You can use flatMap where you can stream over 2nd list and create ObjectCombinations and flatten the list.
List<ObjectCombinations> res =
listOne.stream()
.flatMap(a -> listTwo.stream().map(b -> new ObjectCombinations(a,b)))
.collect(Collectors.toList());
This question already has answers here:
Chaining Optionals in Java 8
(10 answers)
Closed 2 years ago.
public static BigDecimal calculateSomething(List<Type> myList, Optional<Type> secondOne) {
return myList.stream()
.findFirst()
.map(x -> x.getBalance().subtract(x.getAmount()))
.orElse(secondOne.map(x -> x.getBalance().subtract(x.getAmount()))
.orElse(BigDecimal.ZERO));
}
I want to do some mapping on firstOne from myList if it's present. If it's not I want to do same thing on the secondOne. If it's not present either then return ZERO.
Is there a way to write this inside of one stream and reduce code duplication and stream inside of the stream on Optional?
Yep:
return myList.stream().findFirst()
.or(() -> secondOne)
.map(x -> x.getBalance().subtract(x.getAmount()))
.orElse(BigDecimal.ZERO));
This question already has answers here:
How can I turn a List of Lists into a List in Java 8?
(12 answers)
Closed 5 years ago.
I would like to map each entry in my list by calling expand(), which returns multiple entries, and then collect the result as a list.
Without streams, I would accomplish this like:
List<String> myList = new ArrayList<>();
List<String> expanded = new ArrayList<>();
for (String s : myList) {
expanded.addAll(expand(s));
}
return expanded;
private List<String> expand(String x) {
return Arrays.asList(x, x, x);
}
How can I accomplish this with streams? This gives a compilation error:
return myList.stream().map(this::expand).collect(Collectors.toList());
flatMap should help you :
return myList.stream()
.flatMap(x -> expand(x).stream())
.collect(Collectors.toList());
return myList.stream().map(this::expand).collect(Collectors.toList());
returns List<List<String>> because myList.stream().map(this::expand) returns a stream typed as Stream<List<String>> as you pass to map() a variable declared List<String> variable and not String.
You don't want that.
So chain Stream.map() with Stream.flatMap() to amalgamate Stream<List<String>> to Stream<String> :
return myList.stream()
.map(this::expand)
.flatMap(x->x.stream())
.collect(Collectors.toList());
use flatMap to convert the Stream<List<String> to a Stream<String>:
return myList.stream().map(this::expand).flatMap(Collection::stream).collect(Collectors.toList());
This question already has answers here:
Convert List of List into list in java
(5 answers)
Closed 5 years ago.
I have a multimap Map<T,List<L>> map and I need a list with all the values of the values from the map, namely List<L>. With map.values() I get a List<List<L>>, but thats not what I want.
Does someone know a clean solution without looping?
If you are using Java 8, you could collect all L values from all List<L>s in a single List<L> by Stream#flatMap:
final List<L> list = map
// get a Collection<List<L>>
.values()
// make a stream from the collection
.stream()
// turn each List<L> into a Stream<L> and merge these streams
.flatMap(List::stream)
// accumulate the result into a List
.collect(Collectors.toList());
Otherwise, a for-each approach with Collection#addAll can be applied:
final List<L> list = new ArrayList<>();
for (final List<L> values : map.values()) {
list.addAll(values);
}
This question already has answers here:
Java 8 List<V> into Map<K, V>
(23 answers)
Closed 5 years ago.
I have following situation. (pseudocode)
class A {
id;
List<B> bs;
}
class B {}
I wonder how to convert List os As -> Map of Bs
List<A> as;
// the Map key is A.id (Map<A.id, List<B>>)
Map<Integer, List<B>> bs = as.stream()
.map(a ->a.getBs())
.collect(// I dont know what to add here ???);
Seems like you want sometime like this:
Map<Integer, List<B>> bs = as.stream()
.collect(Collectors.toMap(A::getId, A::getBs));