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));
Related
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 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()));
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());
This question already has answers here:
Java collections convert a string to a list of characters
(11 answers)
Closed 7 years ago.
I can propose few answers myself, but they are very far from being elegant.
Prove me that Java is not that hopeless. Thanks.
In Java 8+, you might use a forEach and something like
String str = "Hello";
Set<Character> set = new LinkedHashSet<>();
str.chars().forEach(e -> set.add((char) e));
System.out.println(set);
which outputs
[H, e, l, o]
This question already has answers here:
Sorting HashMap by values [duplicate]
(12 answers)
Closed 9 years ago.
I have a java Map
Map<String , String>
The map is like following
olah : 3
vola : 2
sola : 5
jingle : 9
i want to sort the map on the value string like sort on 3,2,5,9 for example...is there any efficient way possible.
I also want to know what difference it will make if i put a map with same values but
like
Map<String , long>
Does it improve any performance...?
See: Sort a Map<Key, Value> by values (Java)
Something like
Map<String , long>
is not possible because Java Collections can not store primitive types. If you want to do that, you can use fastutil http://fastutil.di.unimi.it/ or trove http://trove.starlight-systems.com/. Memory consumption will be better, since no Long objects are created.