This question already has an answer here:
Partition java streams in categories [duplicate]
(1 answer)
Closed 5 years ago.
This question is a sort-of follow-up to Partition java streams in categories
I have a stream<A>, where
class A {
String category();
String data();
}
I would like to get a map<String, list<String>>, where the original stream is partitioned into sublists based on the value of category(), and then mapped to only extract the data(). It is pretty trivial to have it implemented using a for loop, but is it possible to get a more elegant solution harnessing java streams?
EXAMPLE:
Given {[a, xyz], [a, zyx], [b, abc]}, I would like to get a map:
a -> {xyz, zyx}
b -> {abc}
You need a slightly different groupBy
stream.collect(Collectors.groupingBy(A::getCategory,
Collectors.mapping(A::data, Collectors.toList()));
Related
This question already has answers here:
Is there a concise way to iterate over a stream with indices in Java 8?
(24 answers)
Closed 3 years ago.
I am trying to iterate over a list of maps in java, but I can not figure it out how. I need to keep the index as well.
for example, I have a list of maps like
List<Map<String, String>> ListOfMaps = new ArrayList<>();
and the output that I want is something like:
"ListOfMaps[index].map(k) => ListOfMaps[index].map(k).getValue()"
I appreciate if you can give me some hint how I can do it using java stream operators, with high performance.
As an advocate for streams, I personally don't like using them here. But I will anyhow:
IntStream.range(0, mapList.size())
.forEach(index -> {
Map<String, String> map = mapList.get(index);
System.out.println("ListOfMaps[" + index + "]" ...);
})
This question already has answers here:
Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip)
(14 answers)
Is there an accepted Java equivalent to Python's zip()? [duplicate]
(1 answer)
Closed 5 years ago.
Say I have two streams:
Stream<String> ids = Stream.of("id1","id2","id3","id4","id5");
Stream<MyObj> objects = Stream.of(new MyObj(null, "some data"), new MyObj(null, "some other data");
Now I would like to update my objects stream with the data from ids. The result whould be equivalent to the following Stream:
Stream<MyObj> objects = Stream.of(new MyObj("id1", "some data"), new MyObj("id2", "some other data");
I thus wonder if there is a way to consume both streams, one element at the time. I imagine some kind of "DoubleConsumer" (nothing to do with double) of the sort:
Stream<MyObj> result = DoubleConsumer.of(ids, objects)
.map((id, myobj) -> combine(id, myobj));
MyObj combine(String id, MyObj myobj) {
myobj.set(id);
return myobj;
}
Any idea how to achieve something like this?
Update
I know that I can solve this in the case of list with some double loop, or with FuncionalJava's zipfunction. However the question is how to do this with Java Streams.
Do you want Stream.map. eg.
Stream<MyObj> objs = ids.map(id->new MyObj(id, "some data"));
This does exactly what op wants. Then if they want to update the values of the objects they can afterwards, and the objects know their id so that can be gotten.
If they want to "zip" two streams, they should check the existing answers.
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.
Imaging object with following method:
class A { List<B> getIds(){...} }
Now I have an Collection of A as input;
And I want to get set of unique Ids out of it, normally you would go for:
Set<B> ids = new HashSet<>();
for(A a : input){
ids.addAll(a.getIds());
}
Is there a way to do the same in one line using stream API, like following
Set<List<B>> set = input.stream().map((a) -> a.getIds()).collect(Collectors.toSet());
but making flat set of B
You have to use flatMap
input.stream()
.map(a -> a.getIds())
.flatMap(ids -> ids.stream())
.collect(Collectors.toSet());
This will produce flat Set.
This question already has answers here:
Java 8 List<V> into Map<K, V>
(23 answers)
Closed 5 years ago.
I have a stream<A>, where
class A {
String category();
// ...
}
I would like to get a map<String, list<A>>, where the original stream is partitioned into sublists based on the value of category(). It is pretty trivial to have it implemented using a for loop, but is it possible to get a more elegant solution harnessing java streams?
EXAMPLE:
Given {[a, xyz], [a, zyx], [b, abc]}, I would like to get a map:
a -> {[a, xyz], [a, zyx]}
b -> {[b, abc]}
Use the groupingBy collector.
stream.collect(Collectors.groupingBy(A::category));
This question already has answers here:
Java - using Map how to find matching Key when Values are stored a List of strings
(4 answers)
Closed 6 years ago.
I have a Map. Let's say
Map>
I want to collect all the long values (keys)
when at least one myObj answers
myObj.isEnabled=false
using java stream.
I tried
map.entrySet().stream().filter(entry->entry.getValue().stream().filter(x->!x.isEnabled())).findAny().collect()
List<Long> keys = map.entrySet()
.stream()
.filter(e -> e.getValue().stream().anyMatch(o -> !o.isEnabled()))
.map(Map.Entry::getKey)
.collect(Collectors.toList());