I have map of maps
Map<String, Map<String,Integer>> outerMap = new HashMap<String, Map<String, Integer>>();
and I want to put some values to inner map. Is that correct way? Or it can be done better?
class SampleMap {
Map<String, Map<String, Integer>> outerMap = new HashMap<String, Map<String, Integer>>();
public void add(String outerKey, String innerKey, Integer value) {
Map<String, Integer> tempMap = new HashMap<String, Integer>();
if (outerMap.size() > 0)
tempMap = outerMap.get(outerKey);
tempMap.put(innerKey, value);
outerMap.put(key, tempMap);
}
}
You can improve the code by avoiding the creation of a new inner map eagerly, until the point when you know that you must create it.
In addition, if you know that the inner map instance came from the outer map, you don't have to spend time putting it back where it came from.
public void add(String outerKey, String innerKey, Integer value) {
Map<String, Integer> tempMap
if (outerMap.containsKey(outerKey)) {
tempMap = outerMap.get(outerKey);
} else {
tempMap = new HashMap<String, Integer>();
outerMap.put(outerKey, tempMap);
}
tempMap.put(innerKey, value);
}
Technically there is nothing wrong in your code (except a minor improvement suggested by dasblinkenlight), but is map of maps what you really need?
If you want to read/write values by two keys, probably it's better to create map from pair of two keys (MultiKey or Pair implementation can be used) or another data structure (see this comment for details https://stackoverflow.com/a/3093993/554281)
Related
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())
I have an outerMap which contains an innerMap for each key it got. At first, every innerMap is the same (here, they contain {1=1}.
I want to change the value of one certain innermap, for a certain key.
Here is my code:
public class HelloWorld
{
public static void main(String args[]){
HashMap<String, HashMap<String, Integer>> outerMap = new HashMap<String, HashMap<String, Integer>>();
HashMap<String, Integer> innerMap = new HashMap<String, Integer>();
outerMap.put("1001",innerMap);
outerMap.put("1002",innerMap);
outerMap.put("1003",innerMap);
innerMap.put("1", 1);
//My attempt to change only one innermap;
Map<String, Integer> map_to_change = outerMap.get("1001");
map_to_change.put("1", 0);
//And then I print them to see if it's working;
for(Map.Entry map : outerMap.entrySet() )
{
System.out.println(map.getKey()+" "+map.getValue());
}
}
}
However, the output here is
1003 {1=0}
1002 {1=0}
1001 {1=0}
Which shows that my code changes all innermaps, and not only the one linked with the key "1001".
What can I do?
You are pointing the same innerMap object in the outerMap,
outerMap.put("1001",new HashMap<String, Integer>());//create separate maps
outerMap.put("1002",new HashMap<String, Integer>());
outerMap.put("1003",new HashMap<String, Integer>());
HashMap<String, Integer> innerMap =outerMap.get("1001");//get the map you want to put value
innerMap.put("1", 1);//assign the value
Update:
If you want to retain a copy of Map which you have already created, you can copy and create a new Map from it using putAll method,
outerMap.put("1001",copyMap(innerMap));
outerMap.put("1002",copyMap(innerMap));
outerMap.put("1003",copyMap(innerMap));
copyMap method looks like,
private static HashMap<String, Integer> copyMap(HashMap<String, Integer> innerMap){
HashMap<String, Integer> copiedInnerMap = new HashMap<String, Integer>();
copiedInnerMap.putAll(innerMap);
return copiedInnerMap;
}
Is there a way to define a HashMap that has another HashMap as the value without warning?
I mean if I use generics I would have to define:
HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer,HashMap etc>> map = new HashMap<>();
Is the correct/only way to do it via the followin?
HashMap<Integer, HashMap> map = new HashMap<Integer, HashMap>();
Update based on comments:
I am just reviewing generics and I was under the impression that it is not uncommon to have a hashmap as a value of another hashmap.
Update based on #JimGarrison comment:
Using a hash of hashes is a very common structure in other languages so I am surprised that I need to actually give some specific use case in order for my question to make sense. If I need to give a real example that this could be used, one would be to e.g. navigate through some hierarchical structure. So we could "mimic" a tree.
You might find F-bound types useful, at least from a theoretical point of view. In your case, this might be:
class FBoundedMap<K> extends HashMap<K, FBoundedMap<K>> {
}
Then you could use it this way:
FBoundedMap<Integer> map = new FBoundedMap<>();
FBoundedMap<Integer> inner1 = new FBoundedMap<>();
map.put(1, inner1);
FBoundedMap<Integer> inner2 = new FBoundedMap<>();
map.put(2, inner2);
FBoundedMap<Integer> innerMost1 = new FBoundedMap<>();
inner1.put(11, innerMost1);
FBoundedMap<Integer> innerMost2 = new FBoundedMap<>();
inner2.put(22, innerMost2);
System.out.println(map); // {1={11={}}, 2={22={}}}
You could only store empty maps at the end, and maps of maps in the middle, so the only practical use I see to this is to store data in the keys (in this case these would be Integers) and use the values to keep references to children nodes of a tree structure.
Another way would be to let the values be of any type, including HashMap. This way, you could store maps as values of other maps. In this case, you'd need to declare your maps as:
Map<Integer, Object> map = new HashMap<>();
Map<Integer, Object> inner1 = new HashMap<>();
map.put(1, inner1);
Map<Integer, Object> inner2 = new HashMap<>();
map.put(2, inner2);
Map<Integer, Object> innerMost1 = new HashMap<>();
inner1.put(11, innerMost1);
Map<Integer, Object> innerMost2 = new HashMap<>();
inner2.put(22, innerMost2);
System.out.println(map); // {1={11={}}, 2={22={}}}
Of course, if you need to get a value, you'd need to cast:
Map<Integer, Object> value = (Map<Integer, Object>) map.get(1);
System.out.println(value); // {11={}}
Hi everyone this is the question
I have something like that
private Map<String, Map<String, Double>> map1 = new HashMap<String, Map<String,Double>>();
private Map<String, Map<String, Double>> map2= new HashMap<String, Map<String,Double>>();
private Map<String, Map<String, Double>> map3= new HashMap<String, Map<String,Double>>();
Map1, Map2 and Map3 are of the same type, but depending on factors the data will be agrupated in those maps.
Then I have this code to put the data on each map, acording to the discrimnatign factor
private void doSomething(data){
if(factor1){
map1.put(data);
functionForData(map1);
}
else if(factor2){
map2.put(data);
functionForData(map2);
}
else if(factor3){
map3.put(data);
functionForData(map3);
}
}
I think this isn't the better approach to handle the data and determine which map will store the information, specially because I have to repeat all the code for the functionForData() only changing the map that I need.
How can I improve this?
Thanks a lot!!
This addresses your "duplication of code" issue:
private void doSomething(data, Map<String, Map<String, Double>> map1) {
map.put(data);
functionForData(map);
}
private void doSomething(data){
if(factor1){
doSomething(map1);
}
else if(factor2){
doSomething(map2);
}
else if(factor3){
doSomething(map3);
}
}
Let's say I got a Map<String, String> and I wanna remove all the entries that value contains foo. What is the best way to do it, in terms of optimization/memory/etc.? The four syso below are printing the same result, that is to say {n2=bar}.
public static void main(String[] args) {
Map<String, String> in = new HashMap<String, String>();
in.put("n1", "foo");
in.put("n2", "bar");
in.put("n3", "foobar");
// 1- create a new object with the returned Map
Map<String, String> in1 = new HashMap<String, String>(in);
Map<String, String> out1 = methodThatReturns(in1);
System.out.println(out1);
// 2- overwrite the initial Map with the returned one
Map<String, String> in2 = new HashMap<String, String>(in);
in2 = methodThatReturns(in2);
System.out.println(in2);
// 3- use the clear/putAll methods
Map<String, String> in3 = new HashMap<String, String>(in);
methodThatClearsAndReadds(in3);
System.out.println(in3);
// 4- use an iterator to remove elements
Map<String, String> in4 = new HashMap<String, String>(in);
methodThatRemoves(in4);
System.out.println(in4);
}
public static Map<String, String> methodThatReturns(Map<String, String> in) {
Map<String, String> out = new HashMap<String, String>();
for(Entry<String, String> entry : in.entrySet()) {
if(!entry.getValue().contains("foo")) {
out.put(entry.getKey(), entry.getValue());
}
}
return out;
}
public static void methodThatClearsAndReadds(Map<String, String> in) {
Map<String, String> out = new HashMap<String, String>();
for(Entry<String, String> entry : in.entrySet()) {
if(!entry.getValue().contains("foo")) {
out.put(entry.getKey(), entry.getValue());
}
}
in.clear();
in.putAll(out);
}
public static void methodThatRemoves(Map<String, String> in) {
for(Iterator<Entry<String, String>> it = in.entrySet().iterator(); it.hasNext();) {
if(it.next().getValue().contains("foo")) {
it.remove();
}
}
}
The best way is methodThatRemoves because:
In terms of memory consumption: it doesn't create a new map so doesn't add memory overhead.
In terms of CPU use: iterator has O(1) complexity for calling next or removing the current element.
The most efficient way methodThatRemoves, because it
Uses almost no memory
Creates no objects except the (lightweight) iterator
Is extremely fast (doesn't use any map lookups)
I would not make a copy first though, unless you have an unmodifiable map or you need to preserve the original.
For me the best is the one with the Iterator - methodThatRemoves, because you don't create an intermediate Map and don't use put method.
By the way the first one : methodThatReturns can be faster because put complexity is O(1) whereas remove is O(n) in worst case but it will use more memory because you have 2 different instances of Map.
I would personally go with methodThatRemoves because you are only performing a loop operation and checking for "foo" equality. The others do that as well as Object map creation and map clear/put operations. So you clearly have 1 method doing less.
Also if you want to reduce memory usage you are better off not creating an extra HashMap just to remove 1 or more entries. This is assuming you don't mind the extra computation to iterate the map.
If you really want to go more in depth, you should evaluate this using a profiler or some sort.