Java streams compare object from two streams and create new Object [duplicate] - java

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());

Related

Java Group objects by values in object's list [duplicate]

This question already has answers here:
How to convert List<V> into Map<K, List<V>>, with Java 8 streams and custom List and Map suppliers?
(3 answers)
Converting List<MyObject> to Map<String, List<String>> in Java 8 when we have duplicate elements and custom filter criteria
(4 answers)
How to convert List<String> to Map<String,List<String>> based on a delimeter
(5 answers)
Closed 2 years ago.
Problem
I have a list of objects like:
Class MyObj {
private List<Integer> categories;
private String name;
}
I want to map the list of objects to a Map<Integer, List<MyObj>> using a single stream chain.
Example
MyObj obj1 = new MyObj("name1", Arrays.asList(1, 2, 3));
MyObj obj2 = new MyObj("name2", Arrays.asList(1, 4, 3));
MyObj obj3 = new MyObj("name3", Arrays.asList(4));
List<MyObj> objsList = Arrays.asList(obj1, obj2, obj3);
// Here is what Im trying to accomplish:
// a map like -> **{1: [obj1, obj2], 2: [obj1], 3: [obj1, obj2], 4: [obj2, obj3]}**
Map<Integer, List<MyObj>> = objsList.stream
...help
Looking for a map -> {1: [obj1, obj2], 2: [obj1], 3: [obj1, obj2], 4: [obj2, obj3]}
I think the answer is obvious, but I cant seem to get it to work and having a hard time searching. Thank you in advance
You can stream the List from every MyObj and collect Integer and MyObj as pair and then use Collectors.groupingBy
Map<Integer,List<MyObj>> result = objsList.stream()
.flatMap(obj->obj.getCategories().stream().map(i-> Map.entry(i,obj)))
.collect(Collectors.groupingBy(Map.Entry::getKey,Collectors.mapping(Map.Entry::getValue,Collectors.toList())));
Note : Map.entry is from java 9, you can use new AbstractMap.SimpleEntry<Integer, MyObj>(i, obj) for java 8

List<String> to Map using Lambda expressions [duplicate]

This question already has answers here:
How can I initialize an ArrayList with all zeroes in Java?
(5 answers)
Closed 4 years ago.
I want to transform a list of String to a map, where the key of map is a simple increment.
For example:
List<String> result = new ArrayList<String>();
result.add("hello");
result.add("Java");
Pretend result:
Map<Integer, String> mapOfList;
map(1, "Hello");
map(2, "Java");
Try:
AtomicInteger atomic=new AtomicInteger(0);
mapOfList=result.stream().collect(atomic.incrementAndGet(), s -> s);
You need to iterate. Here's one-line using an int stream:
IntStream.range(0, fillMyList.size()).forEach(i -> fillMyList.set(i, ""));

How to collect from a stream which maps to multiple results? [duplicate]

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());

How to add values from Map<T,List<L>> map to List<L>? [duplicate]

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);
}

Java8 Lambda: concat list [duplicate]

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());

Categories