I have Map<Long, Map<String, String>> map, and I have to filter that by key and further get only value.
I'm trying to do some like that:
Map<Object, Object> resultMap = map.entrySet().stream()
.filter(x -> x.getKey().equals(filterValue))
.map(Map.Entry::getValue).collect(Collectors.toMap(k -> k,v -> v));
But I got Map<Object, Object> map instead of Map<String, String>.
Maybe, there is some better way to do it.
The following should work as you need:
Map<String, String> resultMap = map.entrySet().stream()
.filter(x -> x.getKey().equals(filterValue))
.flatMap(entry -> entry.getValue().entrySet().stream())
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
You should set values of map to specific types in collector
.collect(Collectors.toMap(Object::toString, Object::toString))
From map.entrySet().stream().filter(x -> x.getKey().equals(filterValue)), it could be understood that filterValue is a long. There is no need to stream a map to filter out the values matching a particular key. Because map keys are unique, and there will be only one matching key. You could have just used:
Map<String, String> result = map.get(filterValue);
Related
Map<String, String> preFilteredKafkaRecords = kafkaRecordMap.entrySet().stream()
.map(item -> getUrls(item.getKey(), item.getValue()))
.filter(THING_1)
.filter(THING_2)
.collect(Collectors.toMap(
Map.Entry<String, String>::getKey, Map.Entry<String, String>::getValue));
getUrls - returns Map<String, String>
how can I collect this to a Map<String, String>? The first map returns a Map<String, String> but I can't get the compiler to stop complaining.
If getUrls() returns a Map, you can't collect it as an Entry. If the idea is to combine all the maps, you can use flatMap() to merge their entries into one stream:
Map<String, String> preFilteredKafkaRecords = kafkaRecordMap.entrySet().stream()
.map(item -> getUrls(item.getKey(), item.getValue()))
.map(Map::entrySet)
.flatMap(Set::stream)
.filter(THING_1)
.filter(THING_2)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Alternatively, you can use a custom collector to fold each result into a single map:
Map<String, String> preFilteredKafkaRecords = kafkaRecordMap.entrySet().stream()
.map(item -> getUrls(item.getKey(), item.getValue()))
.filter(THING_1)
.filter(THING_2)
.collect(HashMap::new, Map::putAll, Map::putAll);
I have a List<Map<String,String>>
such as:
Map<String, String> m1 = new HashMap<>();
m1.put("date", "2020.1.5");
m1.put("B", "10");
Map<String, String> m2 = new HashMap<>();
m2.put("date", "2020.1.5");
m2.put("A", "20");
Map<String, String> m3 = new HashMap<>();
m3.put("date", "2020.1.6");
m3.put("A", "30");
Map<String, String> m4 = new HashMap<>();
m4.put("date", "2020.1.7");
m4.put("C", "30");
List<Map<String, String>> before = new ArrayList<>();
before.add(m1);
before.add(m2);
before.add(m3);
before.add(m4);
My expect result is to generate a new List map, which is grouped by date , and all the entry set in the same date would be put together, like:
[{"A":"20","B":"10","date":"2020.1.5"},{"A":"30","date":"2020.1.6"},{"C":"30","date":"2020.1.7"}]
I tried with the following method, but always not my expect result.
stream().flatmap().collect(Collectors.groupingBy())
Some Additional Comments for this problem:
I worked this out with for LOOP, but the application hangs when the list size is about 50000, so I seek a better performant way to do this. Java 8 stream flat map is a perhaps way as far as I know.
So the key point is not only to remap this but also with the most performant way to do this.
before
.stream()
.collect(Collectors.toMap((m) -> m.get("date"), m -> m, (a,b) -> {
Map<String, String> res = new HashMap<>();
res.putAll(a);
res.putAll(b);
return res;
}))
.values();
This is the solution you're looking for.
The toMap function receives 3 parameters:
the key mapper, which in your case is the date
the value mapper, which is the map itself that's being processed
the merge function, which takes 2 maps with the same date and puts all the keys together
Output:
[{date=2020.1.5, A=20, B=10}, {date=2020.1.6, A=30}, {date=2020.1.7, C=30}]
You can do this way using groupingBy and Collector.of
List<Map<String, String>> list = new ArrayList<>(before.stream()
.collect(Collectors.groupingBy(
k -> k.get("date"),
Collector.of( HashMap<String,String>::new,
(m,e)-> m.putAll(e),
(map1,map2)->{ map1.putAll(map2); return map1;}
))).values());
Here, first use Collectors.groupingBy to group by date. Then define custom collector using Collector.of to collect List<Map<String, String>> into Map<String, String>. After create list using map values.
And using Collectors.flatMapping from Java 9
List<Map<String, String>> list = new ArrayList<>(before.stream()
.collect(Collectors.groupingBy(
k -> k.get("date"),
Collectors.flatMapping(m -> m.entrySet().stream(),
Collectors.toMap(k -> k.getKey(), v -> v.getValue(), (a,b) -> a))))
.values());
You can achieve the very same result using a certain number of Collectors, orderly:
Collectors.groupingBy to group by the date
Collectors.reducing to merge the Map<String, String> items
Collectors.collectingAndThen to transform the values from Map<String, Optional<Map<String, String>>>, as a result of the previous reducing to the final output List<Map<String, String>>.
List<Map<String, String>> list = before.stream()
.collect(Collectors.collectingAndThen(
Collectors.groupingBy(
m -> m.get("date"),
Collectors.reducing((l, r) -> {
l.putAll(r);
return l; })
),
o -> o.values().stream()
.flatMap(Optional::stream)
.collect(Collectors.toList())));
The list contains what are you looking for:
[{date=2020.1.5, A=20, B=10}, {date=2020.1.6, A=30}, {date=2020.1.7, C=30}]
Important: This solution has two he disadvantages:
It looks clumsy and might not be clear for an independent viewer
It mutates (modifies) the original maps included in the List<Map<String, String>> before.
It can be done as follows:
List<Map<String, String>> remapped = before.stream()
.collect(Collectors.groupingBy(m -> m.get("date")))
.values().stream()
.map(e -> e.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x1, x2) -> x1)))
.collect(Collectors.toList());
remapped.forEach(System.out::println);
Output:
{date=2020.1.5, A=20, B=10}
{date=2020.1.6, A=30}
{date=2020.1.7, C=30}
I would like to transform a Map<String, List<Object>> so it becomes Map<String, String>. If it were just Map<String, Object> it is easy in Java8;
stream().collect(k -> k.getValue().getMyKey(), Entry::getKey);
But this will not work because getValue returns a List Map<List<Object>, String> in my example. Assume Object contains a getter to be used for the key and that Object does not contain the key in the first map.
Any thoughts?
Stream over the list of objects and extract the key you need then map --> flatten --> toMap
source.entrySet()
.stream()
.flatMap(e -> e.getValue()
.stream()
.map(x -> new SimpleEntry<>(x.getMyKey(), e.getKey())))
.collect(toMap(SimpleEntry::getKey, SimpleEntry::getValue));
use a merge function if there is expected to be duplicate getMyKey() values:
source.entrySet()
.stream()
.flatMap(e -> e.getValue()
.stream()
.map(x -> new SimpleEntry<>(x.getMyKey(), e.getKey())))
.collect(toMap(SimpleEntry::getKey, SimpleEntry::getValue, (l, r) -> l));
Note: the above uses the source map keys as the values of the resulting map as that's what you seem to be illustrating in your post, if however you want the key of the source map to remain as the key of the resulting map then change new SimpleEntry<>(x.getMyKey(), e.getKey()) to new SimpleEntry<>(e.getKey(),x.getMyKey()).
If preference could be to choose any amongst the multiple values mapped as a key, you can simply use:
Map<String, List<YourObject>> existing = new HashMap<>();
Map<String, String> output = new HashMap<>();
existing.forEach((k, v) -> v.forEach(v1 -> output.put(v1.getMyKey(), k)));
Essentially this would put the 'first' such myKey along with its corresponding value which was the key of the existing map.
I want to collect the stream to a LinkedHashMap<String, Object>.
I have a JSON resource that is stored in LinkedHashMap<String, Object> resources.
Then I filter out JSON elements by streaming the EntrySet of this map.
Currently I am collecting the elements of stream to a regular HashMap. But after this I am adding other elements to the map. I want these elements to be in the inserted order.
final List<String> keys = Arrays.asList("status", "createdDate");
Map<String, Object> result = resources.entrySet()
.stream()
.filter(e -> keys.contains(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
result.put("date", "someDate");
return result;
That is why I want to collect the stream to a LinkedHashMap<String, Object>. How can I achieve this?
You can do this with Stream:
Map<String, Object> result = resources.entrySet()
.stream()
.filter(e -> keys.contains(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> y, LinkedHashMap::new));
The part (x, y) -> y is because of mergeFunction when find duplicate keys, it returns value of second key which found. the forth part is mapFactory which a supplier providing a new empty Map into which the results will be inserted.
An alternate way of doing this using Map.forEach is:
Map<String, Object> result = new LinkedHashMap<>();
resources.forEach((key, value) -> {
if (keys.contains(key)) {
result.put(key, value);
}
});
result.put("date", "someDate");
and if you could consider iterating on the keySet as an option:
Map<String, Object> result = new LinkedHashMap<>();
resources.keySet().stream()
.filter(keys::contains)
.forEach(key -> result.put(key,resources.get(key)));
result.put("date", "someDate");
I have a collection of maps:
Collection<Map<String,Double>> myCol = table.values();
I would like to transform this into a Map
Map<String, Double>
such that, for a matching key, values are summed up. Using a for loop, it is rather simple:
Map<String, Double> outMap = new HashMap<>();
for (Map<String, Double> map : myCol) {
outMap = mergeMaps(outMap, map);
}
and mergeMaps() is defined as
mergeMaps(Map<String, Double> m1, Map<String, Double> m2){
Map<String, Double> outMap = new TreeMap<>(m1);
m2.forEach((k,v) -> outMap.merge(k,v,Double::sum)); /*sum values if key exists*/
return outMap;
}
However, I would like to use streams to get a map from collection. I have tried as follows:
Map<String, Double> outMap = new HashMap<>();
myCol.stream().forEach(e-> outMap.putAll(mergeMaps(outMap,e)));
return outMap;
This works without a problem. However, can I still improve it? I mean, how can I use collectors in it?
From your input, you can fetch the stream of maps and then flatmap it to have a Stream<Map.Entry<String, Double>>. From there, you collect them into a new map, specifying that you want to sum the values mapped to the same key.
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.summingDouble;
import static java.util.stream.Collectors.toMap;
....
Map<String, Double> outMap =
myCol.stream()
.flatMap(m -> m.entrySet().stream())
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, Double::sum));
Alternatively, you can use groupingBy instead of toMap:
.collect(groupingBy(Map.Entry::getKey, summingDouble(Map.Entry::getValue)));
myCol.stream()
.flatMap(x -> x.entrySet().stream())
.collect(Collectors.groupingBy(
Entry::getKey,
TreeMap::new,
Collectors.summingDouble(Entry::getValue)));
Well, the other proposed solutions show that a pure stream solution is short, but if you wanted to use your existing mergeFunction (because in other cases it is more complex for example), you could just hand it over to Stream.reduce:
Optional<Map<String, Double>> outMap = myCol.stream().reduce((m1, m2) -> mergeMaps(m1, m2));
Your initial approach with the forEach is pretty much a streamyfied for loop and violates the concept of functions having no side effects. The reduce (or the above collects) handles all the data merging internally, without changing the input collection.
With streams:
Map<String, Double> outMap = myCol.stream()
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(
Map.Entry::getKey, // key of the result map
Map.Entry::getValue, // value of the result map
Double::sum, // how to merge values for equal keys
TreeMap::new)); // the type of map to be created
This uses Collectors.toMap to create the result TreeMap.
You can do it without streams, though. I think your version is a little bit complicated, you could refactor it as follows:
Map<String, Double> outMap = TreeMap<>();
myCol.forEach(map -> map.forEach((k, v) -> outMap.merge(k, v, Double::sum)));
Which is shorter, easy and most readable.