Merge two collections in Java - java

I have two maps:
Map<String, Student> students1 = new HashMap<>();
students1.put("New York", new Student("John"));
students1.put("Canada", new Student("Robert"));
Map<String, Student> students2 = new HashMap<>();
students2.put("Chicago", new Student("Nick"));
students2.put("New York", new Student("Ann"));
As a result, I want to get this:
{Canada=Robert, New York=[John, Ann], Chicago=Nick}
I can easily do it like this:
Map<City, List<Student>> allStudents = new HashMap<>();
students1.forEach((currentCity, currentStudent) -> {
allStudents.computeIfPresent(currentCity, (city, studentsInCity) -> {
studentsInCity.add(currentStudent);
return studentsInCity;
});
allStudents.putIfAbsent(currentCity, new ArrayList<Student>() {
{
add(currentStudent);
}
});
});
// then again for the second list
But is there any other way to merge many collections (two in this case)? Is there something like short lambda expression, or method from some of the integrated java libraries, etc...?

You could create a stream over any number of maps, then flat map over their entries. It is then as simple as grouping by the key of the entry, with the value of the entry mapped to a List as value:
Map<String, List<Student>> collect = Stream.of(students1, students2)
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
With static import for readability:
Map<String, List<Student>> collect = Stream.of(students1, students2)
.flatMap(map -> map.entrySet().stream())
.collect(groupingBy(Entry::getKey, mapping(Entry::getValue, toList())));
Replace toList() with toSet() if a Set is more appropriate as value of the map.

I think the Stream version given by Magnilex is the most elegant way to do this. But I still want to give another choice.
static final Function<...> NEW_LIST = __ -> new ArrayList<>();
Map<City, List<Student>> allStudents = new HashMap<>();
students1.forEach((city, student) -> {
allStudents.computeIfAbsent(city, NEW_LIST).add(student);
});

https://www.baeldung.com/java-merge-maps this is a link, it should help

Related

Java 8 stream grouping a List<Map<>> by the same <Key, Value> to a new List<Map<>>

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}

How do I generate a Map of Map of List (Map<String, Map<Enum, List<String>>>) in java using streams

Given:
enum Food{
FRUITS, VEGGIES;
}
Map<String, List<String>> basketMap = new HashMap<>();
basketMap .put("bucket1", Arrays.asList("apple", "banana"));
basketMap .put("bucket2", Arrays.asList("orange", "kiwi"));
basketMap .put("bucket3", Arrays.asList("banana", "orange"));
Need to generate a Map of map of list(populte fruitBaskerMap)
Map<String, Map<Food, List<String>> fruitBasketMap = new HashMap<>();
Final output:
fruitBasketMap:
[
bucket1, [Food.FRUITS, {"apple", "banana"}],
bucket2, [Food.FRUITS, {"orange", "kiwi"}],
bucket3, [Food.FRUITS, {"banana", "orange"}]
]
I tried the below (but was not successful)
fruitBasketMap = basketMap.entrySet().stream().collect(
Collectors.toMap(Map.Entry::getKey,
Collectors.toMap(Food.FRUITS,
Collectors.toList(Map.Entry::getValue())
)
)
);
Can somebody let me know how do I do that?
This implementation seems to be working (using Java 9 Map.of):
Map<String, Map<Food, List<String>>> fruitBasketMap = basketMap.entrySet().stream()
.map(e -> Map.entry(e.getKey(), Map.of(Food.FRUITS, e.getValue())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Output of the test:
{
bucket2={FRUITS=[orange, kiwi]},
bucket3={FRUITS=[banana, orange]},
bucket1={FRUITS=[apple, banana]}
}
This should do the trick in Java 8+ way:
fruitBasketMap =
basketMap.entrySet()
.stream()
.collect(groupingBy(Entry::getKey,
toMap(e -> Food.FRUITS, Entry::getValue)));
The collector above works in the following way:
Groups the elements by their keys, thus the keys remain the same as in the original map.
Collects the entries into a new map with
key: Food.FRUITS (constant for all entries)
value: the original value (list of strings)
This would work with Java 8
Map<String, Map<Food, List<String>>> collect = basketMap.entrySet().stream().collect(
Collectors.toMap(basketMapEntry -> basketMapEntry.getKey(),
basketMapEntry -> basketMapEntry.getValue().stream()
.collect(Collectors.groupingBy(
item -> type.get(item)
)
)
)
);
I am using the groupingBy collector to group the list of items based on the enum.
Assumption is that there would a Map to know the value of Food based on the entry. Something like:
Map<String, Food> type = new HashMap<>();
type.put("apple", Food.FRUITS);
type.put("banana", Food.FRUITS);
type.put("orange", Food.FRUITS);
type.put("kiwi", Food.FRUITS);
type.put("kiwi", Food.FRUITS);
type.put("potato", Food.VEGGIES);
type.put("carrot", Food.VEGGIES);
You can achieve this in Java-8 way itself. Try the below one. Since the inner map will always have single entry, you can use singletonMap
Map<String, Map<Food, List<String>>> result = basketMap.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> Collections.singletonMap(Food.FRUITS, entry.getValue())));

Merge Two Lists based on a condition and push the result to a map using java 8

I have two lists source and target want to merge them based on some condition and push the data to Hashmap. I tried below code but i could not succeed.
public List<Persona> fetchCommonPersonas(List<User> sourceList,
List<User> targetList) {
final Map<String, String> map = new HashMap<>();
map = sourceList.stream()
.filter(source -> targetList.stream().anyMatch(destination -> {
if(destination.getAge().equals(source.getAge())) {
map.put(source.getUserId(), destination.getUserId());
}
}
));
}
Here's one way of doing it:
Map<String, String> map =
sourceList.stream()
.map(source -> targetList.stream()
.filter(dest -> dest.getUserId().equals(source.getUserId()))
.map(dest -> new SimpleEntry<>(source.getPersonaId(), dest.getPersonaId()))
.firstFirst())
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
You find for each element of the source list a corresponding element of the target list, map these elements to a Map.Entry that contains the two person Ids, and collect all the entries to a Map.
You can utilize a groupingBy of the source list to look up for the data in the second stage and then collect the target and source id pairs as follows -
Map<Integer, List<String>> sourceGrouping = sourceList.stream()
.collect(Collectors.groupingBy(User::getAge,
Collectors.mapping(User::getId, Collectors.toList())));
Map<String, String> map = targetList.stream()
.filter(u -> sourceGrouping.containsKey(u.getAge()))
.flatMap(u -> sourceGrouping.get(u.getAge())
.stream().map(s -> new AbstractMap.SimpleEntry<>(s, u.getId())))
.collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey,
AbstractMap.SimpleEntry::getValue));
After i got inputs from Eran this the final piece of code
Map<String, String> commonMap = sourceList.stream()
.flatMap(source -> targetList.stream()
.filter(target -> source.getUserId().equals(target.getUserId()))
.map(target -> new AbstractMap.SimpleImmutableEntry<>(sourcePersona.getPersonaId(), targetPersona.getPersonaId())))
.filter(immutableEntry -> (immutableEntry != null
&& StringUtils.isNotBlank(immutableEntry.getKey()) && StringUtils.isNotBlank(immutableEntry.getValue())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Java8 convert List of Map to List of string

I am using Java8 to achieve the below things,
Map<String, String> m0 = new HashMap<>();
m0.put("x", "123");
m0.put("y", "456");
m0.put("z", "789");
Map<String, String> m1 = new HashMap<>();
m1.put("x", "000");
m1.put("y", "111");
m1.put("z", "222");
List<Map<String, String>> l = new ArrayList<>(Arrays.asList(m0, m1));
List<String> desiredKeys = Lists.newArrayList("x");
List<Map<String, String>> transformed = l.stream().map(map -> map.entrySet().stream()
.filter(e -> desiredKeys.stream().anyMatch(k -> k.equals(e.getKey())))
.collect(Collectors.toMap(e -> e.getKey(), p -> p.getValue())))
.filter(m -> !m.isEmpty())
.collect(Collectors.toList());
System.err.println(l);
System.err.println(transformed);
List<String> values = new ArrayList<>();
for (Map<String,String> map : transformed) {
values.add(map.values().toString());
System.out.println("Values inside map::"+map.values());
}
System.out.println("values::"+values); //values::[[123], [000]]
Here, I would like to fetch only the x-values from the list. I have achieved it but it is not in a proper format.
Expected output:
values::[123, 000]
Actual output:
values::[[123], [000]]
I know how to fix the actual output. But is there any easy way to achieve this issue? Any help would be appreciable.
You do not need to iterate over the entire map to find an entry by its key. That's what Map.get is for. To flatten the list of list of values, use flatMap:
import static java.util.stream.Collectors.toList;
.....
List<String> values = l.stream()
.flatMap(x -> desiredKeys.stream()
.filter(x::containsKey)
.map(x::get)
).collect(toList());
On a side note, avoid using l (lower case L) as a variable name. It looks too much like the number 1.
I’m not sure Streams will help, here. It’s easier to just loop through the Maps:
Collection<String> values = new ArrayList<>();
for (Map<String, String> map : l) {
Map<String, String> copy = new HashMap<>(map);
copy.keySet().retainAll(desiredKeys);
values.addAll(copy.values());
}
Flat map over the stream of maps to get a single stream representing the map entries of all your input maps. From there, you can filter out each entry whose key is not contained in the desired keys. Finally, extract the equivalent value of each entry to collect them into a list.
final List<String> desiredValues = l.stream()
.map(Map::entrySet)
.flatMap(Collection::stream)
.filter(entry -> desiredKeys.contains(entry.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
EDIT
This assumes that if a map has the key "x" it must also has the key "y" so to fetch the corredponding value.
final List<String> desiredValues = l.stream()
.filter(map -> map.containsKey("x"))
.map(map -> map.get("y"))
.collect(Collectors.toList());

Using stream to convert list of strings to list of maps with custom key

Given list of strings like this:
"Y:Yes",
"N:No",
"A:Apple"
I have something like
Map updated = values.stream().map(v -> v.split(":")).collect(Collectors.toMap(v1 -> v1[0],v1->v1.length>1?v1[1]:v1[0]));
But this gives me map as:
{
"Y":"Yes",
"N":"No",
"A":"Apple"
}
How can I get a list of maps as such:
[
{
name:"Y",
display:"Yes"
},
{
name:"N",
display:"No"
},
{
name:"A",
display:"Apple"
}
]
If you are using Java 9, you can use the new immutable map static factory methods, as follows:
List<Map<String, String>> updated = values.stream()
.map(v -> v.split(":"))
.map(a -> Map.of("name", a[0], "display", a[1]))
.collect(Collectors.toList());
As you want to get a List, not a map, your last function call cannot be Collectors.toMap, needs to be Collectors.toList. Now, each invocation to the map method should generate a new Map, so something like this would do:
List updated = values.stream()
.map(v -> {
String[] parts = v.split(":");
Map<String, String> map = new HashMap<>();
map.put("name", parts[0]);
map.put("display", parts[1]);
return map;
)
.collect(Collectors.toList());
Some people would prefer:
List updated = values.stream()
.map(v -> {
String[] parts = v.split(":");
return new HashMap<>() {{
put("name", parts[0]);
put("display", parts[1]);
}};
)
.collect(Collectors.toList());
which creates an extra helper class. Or if you can use Guava:
List updated = values.stream()
.map(v -> {
String[] parts = v.split(":");
return ImmutableMap.of("name", parts[0], "display", parts[1]);
)
.collect(Collectors.toList());
BTW: In the examples I used Listbut the complete type of what you describe would be List<Map<String, String>>.
You can use following if you're still using Java8, if you happen to use Java9 then have a look at Federicos answer:
final List<Map<String,String>> updated = values.stream()
.map(v -> v.split(":"))
.map(arr -> {
Map<String, String> map = new HashMap<>();
map.put("name", arr[0]);
map.put("display", arr[1]);
return map;
})
.collect(Collectors.toList());

Categories