keySet() usage for <String, List<String>> maps - java

I defined a hashmap as follows:
HashMap<String, List<String>> hashmap = new HashMap<String, List<String>>();
And I would like to retrieve the complete list of keys and print them. However, keySet() does not work for maps defined as <String, List<String>>
Set<String, List<String>> keys = hashmap.keySet();
How could I solve this problem?

What you need is not the .keySet(), but rather the .entrySet():
Set<Map.Entry<String, List<String>>> keys = hashmap.entrySet();
More info:
How to efficiently iterate over each entry in a Map?

Related

Map of Map iteration

Im storing 2 map with different structure in single map like below,
Map<String, List<String>> colMap = new HashMap<String, List<String>>();
Map<String, String> appMap = new HashMap<String, String>();
// colMap assigning some values
// appMap assigning some values
Map<String, Map> mainMap = new HashMap<String, Map>();
mainMap.put("appMap", appMap);
mainMap.put("colMap", colMap);
I want to get map one by one and iterate the map.
If I try get map like below, getting error,
.......
Map colMap = map.get("colMap");
for(Entry<String, List<String>> entry : colMap.entrySet())
Error: Type mismatch: cannot convert from element type Object to Map.Entry<String,List<String>>
Why not just create a simple container POJO class (or record in Java 16+) for the two maps instead of mainMap and keep the relevant type-safety which to do it Java-way?
public class MapPojo {
private final Map<String, List<String>> colMap;
private final Map<String, String> appMap;
public MapPojo(Map<String, List<String>> colMap, Map<String, String> appMap) {
this.colMap = colMap;
this.appMap = appMap;
}
// getters, etc.
}
MapPojo mainMap = new MapPojo(colMap, appMap);
Error you are getting because when you are doing map.get operation your reference is Just Map without any Generics which will treated as Object class's reference. You should use generics like below and it will work -
Map<String, List<String>> colMap = map.get("colMap");
for(Entry<String, List<String>> entry : colMap.entrySet())

How to convert HashMap to TreeMap

This question seems to be very popular and yet I couldn't get correct results for my implementation. I had this thread as an example but so far no luck.
Here I have HashMap that I need to convert to TreeMap in order to have key values sorted:
HasMap<String, HashMap<String, SomeBean>> hashMap = (HashMap<String, HashMap<String, SomeBean>>)request.getAttribute("HASHMAP");
After applying iterator I could see results in unsorted order.
Now I want to convert it to TreeMap:
TreeMap<String, TreeMap<String, SomeBean>> treeMap = new TreeMap<String, TreeMap<String, SomeBean>>(hashMap);
Result:
The constructor TreeMap<String,TreeMap<String,SomeBean>>(HashMap<String,HashMap<String,SomeBean>>) is undefined
Well, it seems because i have nested map with my bean class it is not allowing me to create new tree map. It is understandable as I don't expect TreeMap to have constructor that suits my criteria but the question is how do I find workaround for this problem?
Since your maps have incompatible value types, you'll need to convert them manually:
Map<String, Map<String, SomeBean>> treeMap = new TreeMap<>();
for (Map.Entry<String, HashMap<String, Integer>> e : hashMap.entrySet())
treeMap.put(e.getKey(), new TreeMap<>(e.getValue()));
If your code uses the nested map as a regular Map, i.e. it doesn't use any specific method defined in HashMap or TreeMap, you can define the type parameter of your variables using Map instead:
HashMap<String, Map<String, SomeBean>> hashMap =
(HashMap<String, Map<String, SomeBean>>) request.getAttribute("HASHMAP");
TreeMap<String, Map<String, SomeBean>> treeMap =
new TreeMap<String, Map<String, SomeBean>>(hashMap);
Or else, you won't be able to use constructor(Map) or putAll(Map), and you will have to fill in the treeMap manually.

Itreate through two hashmaps to find duplicate key values

I have created two hashmaps:
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
Each map takes in values from text files on my PC
I have found a way to find duplicates in both of the maps:
Set<String> s = new HashSet<String>(map1.keySet());
s.retainAll(map2.keySet());
System.out.println("Duplicates" + s);
The overall program will do the following:
Place the duplicates in a separate hashmap
For example:
map1 contains:
1234,LID4321
918,LID6677
945, LID8765
map2 contains:
1234,LID1234
918,LID5242
2322,LID8837
7777,LID9871
The end result would be:
And mapDuplicates will contain the duplicates:
1234,LID4321
918,LID6677
1234,LID1234
918,LID5242
The keys are equal, therefore they are duplicates

Java putting Hashmap into Treemap

I am currently reading 2 million lines from a textfile as asked in the previous question
Java Fastest way to read through text file with 2 million lines
Now I store these information into HashMap and I want to sort it via TreeMap because I want to use ceilingkey. Is the following method correct?
private HashMap<Integer, String> hMap = new HashMap();
private TreeMap<Integer, String> tMap = new TreeMap<Integer, String>(hMap);
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
treeMap.putAll(hashMap);
Should work anyway.
This would work just fine:
HashMap<Integer, String> hashMap = new HashMap<>();
TreeMap<Integer, String> treeMap = new TreeMap<>(hashMap);
But I wouldn't advise using HashMap to store the input. You end up with two Maps holding the same huge data. Either do it on the fly and add directly into TreeMap or use List to TreeMap conversion.
Also, for even more efficiency consider primitive collections.
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
hashMap.remove(null);
treeMap.putAll(hashMap);
HashMap will allow null but TreeMap not so before adding into Treemap, remove null from keyset

How merge list when combine two hashMap objects in Java [duplicate]

This question already has answers here:
How can I combine two HashMap objects containing the same types?
(17 answers)
Closed 9 years ago.
I have two HashMaps defined like so:
HashMap<String, List<Incident>> map1 = new HashMap<String, List<Incident>>();
HashMap<String, List<Incident>> map2 = new HashMap<String, List<Incident>>();
Also, I have a 3rd HashMap Object:
HashMap<String, List<Incident>> map3;
and the merge list when combine both.
In short, you can't. map3 doesn't have the correct types to merge map1 and map2 into it.
However if it was also a HashMap<String, List<Incident>>. You could use the putAll method.
map3 = new HashMap<String, List<Incident>>();
map3.putAll(map1);
map3.putAll(map2);
If you wanted to merge the lists inside the HashMap. You could instead do this.
map3 = new HashMap<String, List<Incident>>();
map3.putAll(map1);
for(String key : map2.keySet()) {
List<Incident> list2 = map2.get(key);
List<Incident> list3 = map3.get(key);
if(list3 != null) {
list3.addAll(list2);
} else {
map3.put(key,list2);
}
}
create third map and use putAll() method to add data from ma
HashMap<String, Integer> map1 = new HashMap<String, Integer>();
HashMap<String, Integer> map2 = new HashMap<String, Integer>();
HashMap<String, Integer> map3 = new HashMap<String, Integer>();
map3.putAll(map1);
map3.putAll(map2);
You have different type in question for map3 if that is not by mistake then you need to iterate through both map using EntrySet
Use commons collections:
Map<String, List<Incident>> combined = CollectionUtils.union(map1, map2);
If you want an Integer map, I suppose you could apply the .hashCode method to all values in your Map.
HashMap has a putAll method.
Refer this :
http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

Categories

Resources