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;
}
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 two nested maps and I try to merge two nested maps and produce a output of two merged HashMap. Below is the code that I use:
HashMap<String, Object> map = new HashMap<>();
HashMap<String, Object> map1 = new HashMap<>();
map1.put("location", "A");
HashMap<String, Object> map2 = new HashMap<>();
map2.put("geocoordinates", map1);
HashMap<String, Object> map3 = new HashMap<>();
map3.put("TEST", map2);
map.putAll(map3);
HashMap<String, Object> map11 = new HashMap<>();
map11.put("longitude", "B");
HashMap<String, Object> map12 = new HashMap<>();
map12.put("geocoordinates", map11);
HashMap<String, Object> map13 = new HashMap<>();
map13.put("TEST", map12);
map.putAll(map13);
System.out.println(map);
The output that I get is:
{TEST={geocoordinates={longitude=B}}}
But I expected both longitude and location key to be nested inside geocoordinates key but only longitude B is there. So, How can I get the combined. How achieve this?
Do it as follows:
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Object> map = new HashMap<>();
HashMap<String, Object> map1 = new HashMap<>();
map1.put("location", "A");
HashMap<String, Object> map2 = new HashMap<>();
map2.put("geocoordinates", map1);
HashMap<String, Object> map3 = new HashMap<>();
map3.put("TEST1", map2);
map.putAll(map3);
HashMap<String, Object> map11 = new HashMap<>();
map11.put("longitude", "B");
HashMap<String, Object> map12 = new HashMap<>();
map12.put("geocoordinates", map11);
HashMap<String, Object> map13 = new HashMap<>();
map13.put("TEST2", map12);
map.putAll(map13);
System.out.println(map);
}
}
Output:
{TEST2={geocoordinates={longitude=B}}, TEST1={geocoordinates={location=A}}}
Reason: a Map replaces the old value when you put a new value on the same key (in your case, it is TEST). Note that HashMap.putAll() copies all of the mappings from one map into another. In your code, map.putAll(map3) is equivalent of map.put("TEST",map3). And, map.putAll(map13) is equivalent of map.put("TEST",map13) which replaces the earlier value, map3 because of the same key, TEST.
Update: Adding the following update based on the new requirement mentioned in your comment
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Main {
public static void main(String[] args) {
HashMap<String, Object> map = new HashMap<>();
List<HashMap> list=new ArrayList<HashMap>();
HashMap<String, Object> map1 = new HashMap<>();
map1.put("location", "A");
list.add(map1);
HashMap<String, Object> map11 = new HashMap<>();
map11.put("longitude", "B");
list.add(map11);
HashMap<String, Object> map2 = new HashMap<>();
map2.put("geocoordinates", list);
map.put("TEST",map2);
System.out.println(map);
}
}
Output:
{TEST={geocoordinates=[{location=A}, {longitude=B}]}}
Another way of doing it as follows:
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Object> map1 = new HashMap<>();
map1.put("location", "A");
map1.put("longitude", "B");
HashMap<String, Object> map2 = new HashMap<>();
map2.put("geocoordinates", map1);
HashMap<String, Object> map = new HashMap<>();
map.put("TEST", map2);
System.out.println(map);
}
}
Output:
{TEST={geocoordinates={location=A, longitude=B}}}
The behavior you're seeing is correct – namely, if you have a map and store a new value for an existing key, the old value will be lost.
Here's a simple example that isolates what you're doing. In this code, the initial value "one" will be overwritten by "two". This is how maps work. In your case, you're using other maps instead of simple strings, but the behavior is the same – you have one value and you're replacing it with another value.
HashMap<String, Object> map = new HashMap<>();
map.put("TEST", "one");
map.put("TEST", "two");
To retain both "one" and "two", you need to either use different keys (ex: "TEST1" and "TEST2"), or alter one of the nested maps stored under "TEST", or introduce an altogether different data structure (such as a java.util.Set).
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)
I am trying to add an integer to my database but it wont let me.
int count =0;
public void editHighScores(){
HashMap<String, String> queryValues = new HashMap<String, String>();
queryValues.put("totalWins", count);
myDatabase.insertHighScores(queryValues);
}
You have define the Map to accept String keys and String values, so you can'y put an int as value.
If you are not sure the value type, then Keep it as Object like
HashMap<String, Object> queryValues = new HashMap<String, Object>();
Or, if you know values will be always int only, then define like
HashMap<String, Integer> queryValues = new HashMap<String, Integer>();
Okay, HashMap is expecting two strings. Try doing HashMap<String, Integer> = new HashMap<String, Integer>();
So I have a method, setContainerSummaryMap, which takes in a Map<String, Map<String, Integer>>.
I also have a HashMap<Long, HashMap<String, Integer>>, called contIdDestQuanMapSoFar, which I am going to convert into a HashMap<String, HashMap<String, Integer>> with a HashMap<Long, String>, named contIdToScanIdMap, that maps the keys to one another. This method is below:
public HashMap<String, HashMap<String, Integer>> convertContSummaryMap() {
HashMap<String, HashMap<String, Integer>> toRet = new HashMap<String, HashMap<String, Integer>>();
for (Entry<Long, HashMap<String, Integer>> entry : contIdDestQuanMapSoFar.entrySet()) {
toRet.put(contIdToScanIdMap.get(entry.getKey()), entry.getValue());
}
return toRet;
}
The problem is, when I call the method setContainerSummaryMap(currentPlan.convertContSummaryMap()), I get an error, saying that it is not applicable for the arguments HashMap<String, HashMap<String, Integer>>. How would I modify the datatypes for this to work? Thanks.
You can redefine your setContainerSummaryMap method:
public void setContainerSummaryMap(Map<String, ? extends Map<String, Integer>> map)
or, as Louis already suggested change the return type of your convertContSummaryMap to match:
public HashMap<String, Map<String, Integer>> convertContSummaryMap()
To make this work, the type of toRet must be HashMap<String, Map<String, Integer>>. As #SLaks stated, a HashMap<String, Map> is not the same as a HashMap<String, HashMap>.
Of course, the Maps you'll actually put into it will still be HashMaps.