Improvement in java maps management - java

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);
}
}

Related

Creating a stream to intersect two maps based on their value

I have a method named getNames. Its goal: return the names that occur in both of two maps. I tried rewriting this method to a Stream. But, I don't want testOneNames to be modified in this operation. How to rebuild it as a stream?
private Map<String, List<String>> getNames(Map<String, List<String>> testOneNames, Map<String, List<String>> testSecondNames) {
Map<String, List<String>> copyTestOneName = new HashMap<>(testOneNames);
copyTestOneName.values().retainAll(testSecondNames.values());
return copyTestOneName;
}
You could do the following:
private Map<String, List<String>> getNames(Map<String, List<String>> testOneNames, Map<String, List<String>> testSecondNames) {
return testOneNames.entrySet().stream().filter(e -> testSecondNames.containsValue(e.getValue())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

Get values into a map using spring el

I have a value in a properties file that goes
currency.codes=US:USD,IN:INR,AU:AUD
I am looking to get these values into a map with a (key,value) pair like (US,USD) etc using spring el
I'm trying something like
#Value("#{'${currency.codes}'.split(',|:')}")
private Map<String, String> myMap;
This obviously doesn't work. But I would be grateful if anyone can suggest me with such minimal code or any other alternate solution.
There are a lot of properties like this that I need to get into maps.
-TIA
You can write a static helper method and use that in your expression to reduce the complexity of the SpEL code.
public class MapDecoder {
public static Map<String, String> decodeMap(String value) {
Map<String, String> map = new LinkedHashMap<>();
String[] pairs = value.split(",");
for (String pair : pairs) {
String[] parts = pair.split(":");
map.put(parts[0], parts[1]);
}
return map;
}
}
public class MyBean {
#Value("#{T(mypackage.MapDecoder).decodeMap('${currency.codes}')}")
private Map<String, String> myMap;
}

Map of maps - putting value to inner map

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)

Map/HashMap Casting?

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.

java.lang.String cannot be cast to java.util.Map

I'm new to Java, but not new to programming, so as my first project I decided to create a .txt-.csv parser for someone at work. I read each line in the .txt file and separate it into separate Maps for sections, subsections, subsubsections, and the subsubsections' contents. Each Map is then assigned to the Map above it (more on this below). I print everything to it just fine, but when I try to read it I get the following error: "java.lang.String cannot be cast to java.util.Map". The error only appears after the code is run, not while compiling, nor in NetBeans IDE.
My Maps are in the following form with each Object being the Map below it: (Why can't Java make this easy -_- Associative Arrays are all I want)
(Map)array=<string,Object>
(Map)subarray=<String,Object>
(Map)subsubarray=<String,Object>
(Map)subsubcontents=<String,String>
May not be the most efficient way to read this, plan on converting this to recursive function later, but here is my code, copy-pasted from my project. I put comments at where I've found the error to be.
public static Map<String,Object> array=new HashMap<String,Object>();
/* Code for populating the following Maps and pushing them into array
<String,Object>subarray
<String,Object>subsubarray
<String,String>subsubcontents
*/
Set section=array.entrySet();
Iterator sectionI=section.iterator();
while(sectionI.hasNext()) {
Map.Entry sectionInfo=(Map.Entry)sectionI.next();
Map<String,Object> subMap=(Map)sectionInfo.getValue();
Set subSet=subMap.entrySet();
Iterator subI=subSet.iterator();
while(subI.hasNext()) {
Map.Entry subInfo=(Map.Entry)subI.next();
Map<String,Object> subsubMap=(Map)subInfo.getValue();
Set subsubSet=subsubMap.entrySet();
Iterator subsubI=subsubSet.iterator();
while(subsubI.hasNext()) {
System.out.println("test");
Map.Entry subsubInfo=(Map.Entry)subsubI.next();
Map<String,Object> subcontentsMap=(Map)subsubInfo.getValue();
/*
The above line seems to be causing the issues.
If you comment out the rest of this loop (below this comment)
the error will still appear. If you comment out the rest of this loop
(including the line above this comment) it disappears.
Power of deduction my dear Watson.
*/
Set subcontentsSet=subcontentsMap.entrySet();
Iterator keys=subcontentsSet.iterator();
while(keys.hasNext()) {
Map.Entry keyMap=(Map.Entry)keys.next();
}
Iterator values=subcontentsSet.iterator();
while(values.hasNext()) {
Map.Entry valueMap=(Map.Entry)values.next();
}
}
}
}
Any help would be much appreciated. I've been struggling with this for a couple of days now.
I think you need to clean up your generics to start with:
Set<Map.Entry<String, Object>> section = array.entrySet();
Iterator<Map.Entry<String, Object>> sectionI = section.iterator();
while (sectionI.hasNext()) {
Map.Entry<String, Object> sectionInfo = sectionI.next();
Map<String, Object> subMap = (Map<String, Object>) sectionInfo.getValue(); // is this actually a Map<String, Object>?
Set<Map.Entry<String, Object>> subSet = subMap.entrySet();
Iterator<Map.Entry<String, Object>> subI = subSet.iterator();
while (subI.hasNext()) {
Map.Entry<String, Object> subInfo = subI.next();
Map<String, Object> subsubMap = (Map<String, Object>) subInfo.getValue(); // is this actually a Map<String, Object>?
Set<Map.Entry<String, Object>> subsubSet = subsubMap.entrySet();
Iterator<Map.Entry<String, Object>> subsubI = subsubSet.iterator();
while (subsubI.hasNext()) {
System.out.println("test");
Map.Entry<String, Object> subsubInfo = subsubI.next();
Map<String, Object> subcontentsMap = (Map<String, Object>) subsubInfo.getValue(); // somehow a String got in here?
/*
The above line seems to be causing the issues.
If you comment out the rest of this loop (below this comment)
the error will still appear. If you comment out the rest of this loop
(including the line above this comment) it disappears.
Power of deduction my dear Watson.
*/
Set<Map.Entry<String, Object>> subcontentsSet = subcontentsMap.entrySet();
Iterator<Map.Entry<String, Object>> keys = subcontentsSet.iterator();
while (keys.hasNext()) {
Map.Entry<String, Object> keyMap = keys.next();
}
Iterator<Map.Entry<String, Object>> values = subcontentsSet.iterator();
while (values.hasNext()) {
Map.Entry<String, Object> valueMap = values.next();
}
}
}
}
Then, you should be more explicit with your declaration of array:
public static Map<String, Map<String, Map<String, Map<String, String>>>> array = new HashMap<String, Map<String, Map<String, Map<String, String>>>>();
This would ensure that you are putting the correct objects into each of the maps. You will never be able to put a String value where a Map<> is expected because it will not compile. This will allow you to write the following code (without needing casts):
final Set<Map.Entry<String, Map<String, Map<String, Map<String, String>>>>> section = array.entrySet();
final Iterator<Map.Entry<String, Map<String, Map<String, Map<String, String>>>>> sectionI = section.iterator();
while (sectionI.hasNext()) {
final Entry<String, Map<String, Map<String, Map<String, String>>>> sectionInfo = sectionI.next();
final Map<String, Map<String, Map<String, String>>> subMap = sectionInfo.getValue();
final Set<Map.Entry<String, Map<String, Map<String, String>>>> subSet = subMap.entrySet();
final Iterator<Map.Entry<String, Map<String, Map<String, String>>>> subI = subSet.iterator();
while (subI.hasNext()) {
final Map.Entry<String, Map<String, Map<String, String>>> subInfo = subI.next();
final Map<String, Map<String, String>> subsubMap = subInfo.getValue();
final Set<Map.Entry<String, Map<String, String>>> subsubSet = subsubMap.entrySet();
final Iterator<Map.Entry<String, Map<String, String>>> subsubI = subsubSet.iterator();
while (subsubI.hasNext()) {
System.out.println("test");
final Map.Entry<String, Map<String, String>> subsubInfo = subsubI.next();
final Map<String, String> subcontentsMap = subsubInfo.getValue();
final Set<Map.Entry<String, String>> subcontentsSet = subcontentsMap.entrySet();
final Iterator<Map.Entry<String, String>> entries = subcontentsSet.iterator();
while (entries.hasNext()) {
final Map.Entry<String, String> entry = entries.next();
}
}
}
}
All that being said, all of those nested generics look ugly. I'd recommend you create some objects to represent your data.
You can do this :
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement element = gson.fromJson (jsonString, JsonElement.class);
JsonObject jsonObj = element.getAsJsonObject();
Map<String,Object> resultMap = new Gson().fromJson(jsonObj, Map.class);
The exception tells you everything. This call subsubInfo.getValue(); is actually returning a String, not a Map, so you have a logical error when creating your maps.
The compiler will warn you about this if you change your declarations to Map<String, Map> instead of Map<String, Object>

Categories

Resources