I want to be able to convert a List to a HashMap where the key is the elementName and the values is a list of something random (in this case its the Element Name). So in short I want (A->List(A), B->List(B), C-> List(C)). I tried using toMap() and passing it the keyMapper and ValueMapper but I get a compilation error. I would really appreciate if someone can help me out.
Thanks!
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list = Arrays.asList("A","B","C","D");
Map<String, List<String>> map = list.stream().map((element)->{
Map<String, List<String>> map = new HashMap<>();
map.put(element, Arrays.asList(element));
return map;
}).collect(??);
}
Function<Map<String, String>, String> key = (map) -> {
return map.keySet().stream().findFirst().get();
};
Function<Map<String, String>, String> value = (map) -> {
return map.values().stream().findFirst().get();
};
=== This worked for me
Thanks for all the help guys! #izstas "they should operate on the elements" helped a lot :). Actually this is what I was looking for to be exact
public static void test2 (){
Function<Entry<String, List<String>>, String> key = (entry) -> {
return entry.getKey();
};
Function<Entry<String, List<String>>, List<String>> value = (entry) -> {
return new ArrayList<String>(entry.getValue());
};
BinaryOperator<List<String>> merge = (old, latest)->{
old.addAll(latest);
return old;
};
Map<String, List<String>> map1 = new HashMap<>();
map1.put("A", Arrays.asList("A1", "A2"));
map1.put("B", Arrays.asList("B1"));
map1.put("D", Arrays.asList("D1"));
Map<String, List<String>> map2 = new HashMap<>();
map2.put("C", Arrays.asList("C1","C2"));
map2.put("D", Arrays.asList("D2"));
Stream<Map<String, List<String>>> stream =Stream.of(map1, map2);
System.out.println(stream.flatMap((map)->{
return map.entrySet().stream();
}).collect(Collectors.toMap(key, value, merge)));
}
You can use the groupingBy method to manage aggregation, for example:
public static void main(String[] args) {
List<String> list = Arrays.asList("A", "B", "C", "D", "A");
Map<String, List<String>> map = list.stream().collect(Collectors.groupingBy(Function.identity()));
}
If you want more flexibility (for example to map the value and return a Set instead of a List) you can always use the groupingBy method with more parameters as specified in javadoc:
Map<City, Set<String>> namesByCity = people.stream().collect(Collectors.groupingBy(Person::getCity, mapping(Person::getLastName, toSet())));
Functions key and value you have defined in your code are not correct because they should operate on the elements of your list, and your elements are not Maps.
The following code works for me:
List<String> list = Arrays.asList("A", "B", "C", "D");
Map<String, List<String>> map = list.stream()
.collect(Collectors.toMap(Function.identity(), Arrays::asList));
First argument to Collectors.toMap defines how to make a key from the list element (leaving it as is), second argument defines how to make a value (making an ArrayList with a single element).
Thanks for all the help guys! #izstas "they should operate on the elements" helped a lot :). Actually this is what I was looking for to be exact
public static void test2 (){
Function<Entry<String, List<String>>, String> key = (entry) -> {
return entry.getKey();
};
Function<Entry<String, List<String>>, List<String>> value = (entry) -> {
return new ArrayList<String>(entry.getValue());
};
BinaryOperator<List<String>> merge = (old, latest)->{
old.addAll(latest);
return old;
};
Map<String, List<String>> map1 = new HashMap<>();
map1.put("A", Arrays.asList("A1", "A2"));
map1.put("B", Arrays.asList("B1"));
map1.put("D", Arrays.asList("D1"));
Map<String, List<String>> map2 = new HashMap<>();
map2.put("C", Arrays.asList("C1","C2"));
map2.put("D", Arrays.asList("D2"));
Stream<Map<String, List<String>>> stream =Stream.of(map1, map2);
System.out.println(stream.flatMap((map)->{
return map.entrySet().stream();
}).collect(Collectors.toMap(key, value, merge)));
}
Related
I have a Map as below,
Map<String, Map<String, String>> dateFormatsMap;
Map<String,String> m1 = Map.of("A","DD", "B", "MM", "C","YY");
Map<String,String> m2 = Map.of("A","DD-MM", "X", "MM", "C","YYYY");
Map<String,String> m3 = Map.of("X","DD", "Y", "MM", "C","YY");
dateFormatsMap = Map.of("P2",m2,"P1",m1, "p3",m3);
This map is populated from DB and has project-wise data.
What I want is to get merged Map of a specific project and one default project and in case of a duplicate, I want to keep values of a specified project.
I tried below code,
private static Map<String, String> getFormats(String project) {
Set<String> projects = Set.of("P1", project); //e.g "P1" as default project
return dateFormatsMap.entrySet().stream()
.filter(e->projects.contains(e.getKey()))
.flatMap(e->e.getValue().entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(o,n)->o));
}
I call this method as,
getFormats("P2");
But this gives the varying results having values sometimes from "P1" and sometimes from "P1".
{A=DD, B=MM, C=YY, X=MM}
{A=DD-MM, B=MM, C=YYYY, X=MM}
I need to have a fixed result as,
{A=DD-MM, B=MM, C=YYYY, X=MM}
You can check ideone
I hope, I have detailed the question. I appreciate any help.
One way to deal with this without using Streams, you could also seek is using putAll -
private static Map<String, String> getFormats(String project) {
Map<String, String> baseProjFormats = dateFormatsMap.get("P1");
Map<String, String> currentProjFormats = dateFormatsMap.getOrDefault(project, Collections.emptyMap());
baseProjFormats.putAll(currentProjFormats);
return baseProjFormats;
}
I added all from the default project which are not in the specific project map and afterwards added everything from the specific project map:
private static Map<String, String> getMyFormats(String project) {
String defaultProject = "P1";
Map<String, String> specificMap = dateFormatsMap.get(project);
Map<String, String> collect = dateFormatsMap.get(defaultProject).entrySet().stream()
.filter(e -> !specificMap.containsKey(e.getKey()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
collect.putAll(specificMap);
return collect;
}
or using collectingAndThen:
private static Map<String, String> getFormats(String project) {
String defaultProject = "P1";
Map<String, String> collect = dateFormatsMap.get(defaultProject).entrySet().stream()
.filter(e -> !dateFormatsMap.get(project).containsKey(e.getKey()))
.collect(collectingAndThen(
toMap(Map.Entry::getKey, Map.Entry::getValue),
map -> {
map.putAll(dateFormatsMap.get(project));
return map;
})
);
return collect;
}
I got the solution. Created two maps and then merged as below,
private static Map<String, String> getFormats(String project) {
Map<String, String> baseProjFormats = dateFormatsMap.get("P1");
Map<String, String> currentProjFormats = dateFormatsMap.get(project);
return Stream.of(baseProjFormats, currentProjFormats)
.flatMap(e -> e.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(o, n) -> n));
}
I would welcome any better solution.
I'm just going to be the guy who doesn't use streams:
public static void main(String[] args) {
Map<String, String> defaultMap = Map.of("A", "DD", "B", "MM", "C", "YY");
Map<String, String> mapToMerge = Map.of("A", "DD-MM", "X", "MM", "C", "YYYY");
// Do the magic
Map<String, String> result = new HashMap<String, String>();
result.putAll(mapToMerge);
for (Map.Entry<String, String> entry : defaultMap.entrySet())
if (!result.containsKey(entry.getKey()))
result.put(entry.getKey(), entry.getValue());
// Print results
result.entrySet().stream().forEach(System.out::println);
}
I need to invert map which is <String, List<String>> to Map<String,String> using java 8. with assumption that the values are unique. For example,
Input Map -
{"Fruit" -> ["apple","orange"], "Animal" -> ["Dog","Cat"]}
Output Map
{"apple" -> "Fruit", "orange" -> "Fruit", "Dog"->"Animal", "Cat" -> "Animal"}
Map <String, String> outputMap = new HashMap<>();
for (Map.Entry<String, List<String>> entry : inputMap.entrySet()) {
entry.getValue().forEach(value -> outputMap.put(value, entry.getKey()));
}
Is this right? can we achieve this using streams java 8?
You an try this way
Map <String, String> updatedMap = new HashMap<>();
oldMap.keySet()
.forEach(i -> oldMap.get(i)
.forEach(k -> updatedMap.put(k, i)));
Do like this :
public class InverterMap {
public static void main(String[] args) {
Map<String, List<String>> mp = new HashMap<String, List<String>>();
mp.put("Fruit", Arrays.asList("Apple", "Orange"));
mp.put("Animal", Arrays.asList("Dog", "Cat"));
System.out.println(mp); // It returned {Fruit=[Apple, Orange], Animal=[Dog, Cat]}
Map<String, String> invertMap = mp.entrySet().stream().collect(HashMap::new,
(m, v) -> v.getValue().forEach(k -> m.put(k, v.getKey())), Map::putAll);
System.out.println(invertMap);// It returned {Apple=Fruit, Cat=Animal, Orange=Fruit, Dog=Animal}
}
}
Read Stream.collect(Supplier supplier, BiConsumer, BiConsumer combiner) for more info.
I am trying to check whether a map contains all contents of another map. For example, I have a mapA which is a Map<String, List<String>> and the elements are:
"1" -> ["a","b"]
"2" -> ["c","d"]
another mapB which is also a Map<String, List<String>>, the elements are:
"1" -> ["a"]
"2" -> ["c","d"],
I want to create a function compare(mapA, mapB) which will return false in this case.
What is the best way to do this?
Inside your compare(mapA, mapB) method, you can simply use:
return mapA.entrySet().containsAll(mapB.entrySet());
The answer provided by #Jacob G wont work in your case. It will work only if there is an extra (key, value) pair in MapA. like
MapA = {"1" -> ["a","b"] "2" -> ["c","d"] }
and
MapB = {"1" -> ["a","b"] }.
What you need is this:
boolean isStrictlyDominate(LinkedHashMap<Integer, HashSet<Integer>> firstMap, LinkedHashMap<Integer, HashSet<Integer>> secondMap){
for (Map.Entry<Integer, HashSet<Integer>> item : secondMap.entrySet()) {
int secondMapKey = item.getKey();
if(firstMap.containsKey(secondMapKey)) {
HashSet<Integer> secondMapValue = item.getValue();
HashSet<Integer> firstMapValue = firstMap.get(secondMapKey) ;
if(!firstMapValue.containsAll(secondMapValue)) {
return false;
}
}
}
return !firstMap.equals(secondMap);
}
(if you do not want to check strict domination then just return true at last return statement)
Try this code :
Assert.assertTrue(currentMap.entrySet().containsAll(expectedMap.entrySet()));
you can try this.
static boolean compare(Map<String, List<String>> mapA, Map<String, List<String>> mapB){
return mapA.entrySet().containsAll(mapB.entrySet());
}
As suppose, provided data is something like this:
Map<String, List<String>> mapA = new HashMap<>();
Map<String, List<String>> mapB = new HashMap<>();
mapA.put("1", Arrays.asList("a","b"));
mapA.put("2", Arrays.asList("c","d"));
mapB.put("1", Arrays.asList("a"));
mapB.put("2", Arrays.asList("c", "d"));
System.out.println(compare(mapA, mapB));
In this case compare(mapA, mapB) method will return false.
But suppose provided data is something like this:
Map<String, List<String>> mapA = new HashMap<>();
Map<String, List<String>> mapB = new HashMap<>();
mapA.put("1", Arrays.asList("a","b"));
mapA.put("2", Arrays.asList("c","d"));
mapB.put("1", Arrays.asList("a", "b"));
mapB.put("2", Arrays.asList("c", "d"));
System.out.println(compare(mapA, mapB));
In this case, compare(mapA, mapB) method, which I have written will return true.
compare(mapA, mapB) method basically checking for all the entries in mapA with mapB, if same returning yes, else returning false;
I would like to merge two Map with JAVA 8 Stream:
Map<String, List<String>> mapGlobal = new HashMap<String, List<String>>();
Map<String, List<String>> mapAdded = new HashMap<String, List<String>>();
I try to use this implementation:
mapGlobal = Stream.of(mapGlobal, mapAdded)
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue,
Collectors.toList())
));
However, this implementation only create a result like:
Map<String, List<Object>>
If one key is not contained in the mapGlobal, it would be added as a new key with the corresponding List of String. If the key is duplicated in mapGlobal and mapAdded, both list of values will be merge as: A = {1, 3, 5, 7} and B = {1, 2, 4, 6} then A ∪ B = {1, 2, 3, 4, 5, 6, 7}.
You can do this by iterating over all the entries in mapAdded and merging them into mapGlobal.
The following iterates over the entries of mapAdded by calling forEach(action) where the action consumes the key and value of each entry. For each entry, we call merge(key, value, remappingFunction) on mapGlobal: this will either create the entry under the key k and value v if the key didn't exist or it will invoke the given remapping function if they already existed. This function takes the 2 lists to merge, which in this case, are first added to a TreeSet to ensure both unique and sorted elements and converted back into a list:
mapAdded.forEach((k, v) -> mapGlobal.merge(k, v, (v1, v2) -> {
Set<String> set = new TreeSet<>(v1);
set.addAll(v2);
return new ArrayList<>(set);
}));
If you want to run that potentially in parallel, you can create a Stream pipeline by getting the entrySet() and calling parallelStream() on it. But then, you need to make sure to use a map that supports concurrency for mapGlobal, like a ConcurrentHashMap.
ConcurrentMap<String, List<String>> mapGlobal = new ConcurrentHashMap<>();
// ...
mapAdded.entrySet().parallelStream().forEach(e -> mapGlobal.merge(e.getKey(), e.getValue(), (v1, v2) -> {
Set<String> set = new TreeSet<>(v1);
set.addAll(v2);
return new ArrayList<>(set);
}));
Using foreach over Map can be used to merge given arraylist.
public Map<String, ArrayList<String>> merge(Map<String, ArrayList<String>> map1, Map<String, ArrayList<String>> map2) {
Map<String, ArrayList<String>> map = new HashMap<>();
map.putAll(map1);
map2.forEach((key , value) -> {
//Get the value for key in map.
ArrayList<String> list = map.get(key);
if (list == null) {
map.put(key,value);
}
else {
//Merge two list together
ArrayList<String> mergedValue = new ArrayList<>(value);
mergedValue.addAll(list);
map.put(key , mergedValue);
}
});
return map;
}
The original implementation doesn't create result like Map<String, List<Object>>, but Map<String, List<List<String>>>. You need additional Stream pipeline on it to produce Map<String, List<String>>.
Map<String, List<String>> result = new HashMap<String, List<String>>();
Map<String, List<String>> map1 = new HashMap<String, List<String>>();
Map<String, List<String>> map2 = new HashMap<String, List<String>>();
for(Map.Entry<String, List<String>> entry: map1.entrySet()) {
result.put(entry.getKey(), new ArrayList<>(entry.getValue());
}
for(Map.Entry<String, List<String>> entry: map2.entrySet()) {
if(result.contains(entry.getKey())){
result.get(entry.getKey()).addAll(entry.getValue());
} else {
result.put(entry.getKey(), new ArrayList<>(entry.getValue());
}
}
This solution creates independent result map without any reference to map1 and map2 lists.
Using StreamEx
Map<String, List<String>> mergedMap =
EntryStream.of(mapGlobal)
.append(EntryStream.of(mapAdded))
.toMap((v1, v2) -> {
List<String> combined = new ArrayList<>();
combined.addAll(v1);
combined.addAll(v2);
return combined;
});
If you have even more maps to merge just append to the stream
.append(EntryStream.of(mapAdded2))
.append(EntryStream.of(mapAdded3))
Here is the full code to Iterate Two HashMap which has values stored in the form of a list. Merging all the keys and values in first hashmap. Below is the example.
HashMap<String, List<String>> hmap1 = new HashMap<>();
List<String> list1 = new LinkedList<>();
list1.add("000");
list1.add("111");
List<String> list2 = new LinkedList<>();
list2.add("222");
list2.add("333");
hmap1.put("Competitor", list1);
hmap1.put("Contractor", list2);
// System.out.println(hmap1);
HashMap<String, List<String>> hmap2 = new HashMap<>();
List<String> list3 = new LinkedList<>();
list3.add("aaa");
list3.add("bbb");
List<String> list4 = new LinkedList<>();
list4.add("ccc");
list4.add("ddd");
hmap2.put("Competitor", list3);
hmap2.put("Contractor", list4);
//******* Java 8 Feature *****
hmap1.forEach((k, v) -> hmap2.merge(k, v, (v1, v2) -> {
List<String> li = new LinkedList<>(v1);
li.addAll(v2);
hmap2.put(k,li);
return new ArrayList<>(li);
}));
System.out.println(hmap2);
Output:
{Competitor=[aaa, bbb, 000, 111], Contractor=[ccc, ddd, 222, 333]}
I have the following maps.
Map<String,String> map1= new HashMap<String, String>(){{
put("no1","123"); put("no2","5434"); put("no5","234");}};
Map<String,String> map1 = new HashMap<String, String>(){{
put("no1","523"); put("no2","234"); put("no3","234");}};
sum(map1, map2);
I want to join them to one, summing up similar keyed values together. What;s the best way I could do it using java 7 or guava libraries ?
expected output
Map<String, String> output = { { "no1" ,"646"}, { "no2", "5668"}, {"no5","234"}, {"no3","234" } }
private static Map<String, String> sum(Map<String, String> map1, Map<String, String> map2) {
Map<String, String> result = new HashMap<String, String>();
result.putAll(map1);
for (String key : map2.keySet()) {
String value = result.get(key);
if (value != null) {
Integer newValue = Integer.valueOf(value) + Integer.valueOf(map2.get(key));
result.put(key, newValue.toString());
} else {
result.put(key, map2.get(key));
}
}
return result;
}
try this
Map<String, List<String>> map3 = new HashMap<String, List<String>>();
for (Entry<String, String> e : map1.entrySet()) {
List<String> list = new ArrayList<String>();
list.add(e.getValue());
String v2 = map2.remove(e.getKey());
if (v2 != null) {
list.add(v2);
}
map3.put(e.getKey(), list);
}
for (Entry<String, String> e : map2.entrySet()) {
map3.put(e.getKey(), new ArrayList<String>(Arrays.asList(e.getValue())));
}
Java 8 introduces Map.merge(K, V, BiFunction), which makes this easy if not particularly concise:
Map<String, String> result = new HashMap<>(map1);
//or just merge into map1 if mutating it is okay
map2.forEach((k, v) -> result.merge(k, v, (a, b) ->
Integer.toString(Integer.parseInt(a) + Integer.parseInt(b))));
If you're doing this repeatedly, you're going to be parsing and creating a lot of strings. If you're generating the maps one at a time, you're best off maintaining a list of strings and only parsing and summing once.
Map<String, List<String>> deferredSum = new HashMap<>();
//for each map
mapN.forEach((k, v) ->
deferredSum.computeIfAbsent(k, x -> new ArrayList<String>()).add(v));
//when you're done
Map<String, String> result = new HashMap<>();
deferredSum.forEach((k, v) -> result.put(k,
Long.toString(v.stream().mapToInt(Integer::parseInt).sum())));
If this summing is a common operation, consider whether using Integer as your value type makes more sense; you can use Integer::sum as the merge function in that case, and maintaining lists of deferred sums would no longer be necessary.
Try this
Map<String,String> map1= new HashMap<String, String>(){{
put("no1","123"); put("no2","5434"); put("no5","234");}};
Map<String,String> map2 = new HashMap<String, String>(){{
put("no1","523"); put("no2","234"); put("no3","234");}};
Map<String,String> newMap=map1;
for(String a:map2.keySet()){
if(newMap.keySet().contains(a)){
newMap.put(a,""+(Integer.parseInt(newMap.get(a))+Integer.parseInt(map2.get(a))));
}
else{
newMap.put(a,map2.get(a));
}
}
for(String k : newMap.keySet()){
System.out.println("key : "+ k + " value : " + newMap.get(k));
}