how iterate Hash-Map into another method - java

i have this hash map :
HashMap<String, String> meMap = new HashMap<String, String>();
meMap.put(p.getName(), selState);
how can i iterate it into another method?How can i pass map to another method to iterate it?

meMap.entrySet() gives you that ability
You can use it like for (Entry<String, String> entry : meMap.entrySet()) {...} or pass to another method

Related

put is not applicable for HashMap type

Below is my code snippet
Map<Object, Object> gobalMap = new HashMap<Object, Object>();
Map<String, Map<String, Integer>> mp = new HashMap<String, Map<String, Integer>>();
gobalMap.put("mp",mp );
((Map<String, Map<String, Integer>>)gobalMap.get("mp")).put("A", new HashMap<String, Integer>().put("A", 1));
error:
The method put(String, Map<String,Integer>) in the type Map<String,Map<String,Integer>> is not applicable for the arguments (String, Integer)
May I know where am doing wrong ..?
new HashMap<String, Integer>().put("A", 1)
This returns an Integer. But you want to add this to an object which stores Maps and not Integer. So that's not possible. Also as Thomas explained in the comments, your code would not work even if it compiled because put returns the previous value of the map so you will receive a NullPointerException.
I would recommend restructuring your code to make it more readable and to also make it work:
Map<Object, Object> gobalMap = new HashMap<Object, Object>();
Map<String, Map<String, Integer>> mp = new HashMap<String, Map<String, Integer>>();
gobalMap.put("mp",mp );
HashMap<String, Integer> aMap = new HashMap<>();
aMap.put("A", 1);
((Map<String, Map<String, Integer>>)gobalMap.get("mp")).put("A", aMap);
As others have already stated new HashMap<String, Integer>().put("A", 1) returns an Integer (the previously mapped value for key "A" so null in this case) and that is not a suitable value for a Map<String, Map<String, Integer>>.
You're creating a suitable map but don't actually put it into the map so the reference to that map is lost.
Since you're probably trying to only create a nested map if it doesn't exist already try this:
((Map<String, Map<String, Integer>>)gobalMap.get("mp"))
.computeIfAbsent( "A", k -> new HashMap<String, Integer>())
.put("A", 1);
This does the following:
get and cast the map from globalMap (if you'd not be sure this can't return null you could use computeIfAbsent() here as well)
get the nested map for key "A" and if it doesn't exist create a new one, add and return it
put the value 1 for key "A" into the nested map
new HashMap<String, Integer>().put("A", 1) returns an integer, because when you put into a hashmap, you get back the previous value held by that key. As such it cannot be the value in a Map<String,Map<String,Integer>>.
Perhaps you meant to cast gobalMap to a Map<String,Map<String,Integer>>. But you are actually casting gobalMap.get("mp") to a Map<String, Map<String, Integer>>.
This, on the other hand, would compile:
((Map<String, Integer>) gobalMap.get("mp")).put("A", new HashMap<String, Integer>().put("A", 1));
though I'm not sure it does anything useful.
you missed the bracket. correct code will be:
((Map<String, Map<String, Integer>>)gobalMap.get("mp")).put("A", new HashMap<String, Integer>()).put("A", 1);

How to Convert some custom Map object to LinkedHashMap?

Here is what I'm trying to do.
Map<String, List<Address>> mapObj = someService.getSomeAddress();
Using above call I'm getting mapObj of type Map<String, List<Address>>
And I want to send mapObj as a parameter in a another method (that I cannot change) as a LinkedHashMap<String, List<LinkedHashMap>> which does some further processing.
Is there any way that I can solve this problem without affecting data inside mapObj?
You will need to perform a couple of conversion steps.
Convert all Address objects to LinkedHashMap objects.
Put all Map entries into a LinkedHashMap object.
For 1st one, you can write a utility method some where that can do this conversion. For example,
public static List<LinkedHashMap> addresstoMap(List<Address> addresses)
{
List<LinkedHashMap> list = new ArrayList<>();
for(Address a: addresses){
LinkedHashMap map = new LinkedHashMap();
// Add address fields to map here
list.add(map);
}
return list;
}
Then, for the 2nd step, you can do this:
LinkedHashMap<String, List<LinkedHashMap>> map = new LinkedHashMap<?,?>();
iterate through the entry sets of mapObj and put them into the above map object.
for (Map.Entry<String, List<Address>> e : m.entrySet()) {
map.put(e.getKey(), addresstoMap(e.getValue()));
}
The final map object above will contain the correct representation in the LinkedHashMap<String, List<LinkedHashMap>> datatype.
Hope this helps!

How to Extract a Map object from another Map object Java

I have the following data structure:
Map<String,Map<String,String>>
I'd like to extract its value (which itself is another string Map) from this complex Map object. I am currently doing it as such:
Map<String, Map<String, String>> map = getStructure(data,format);
Map<String,String> newMap = new LinkedHashMap<String, String>();
for(Entry<String, Map<String,String>> entry : map.entrySet()) {
for (Entry<String, String> value : entry.getValue().entrySet()) {
newMap.put(value.getKey(),value.getValue());
}
}
The above implementation gives me a new Map object with repeating key-value pairs due to the outer foreach loop, it is iterating through. Seems like I'm missing something.
How can I extract the inner Map object from the complex Map object?
Edit:
Addressing AlexWien's comment
Original Data Structure:
The reasoning behind the original data structure is to store a single value for a pair of IDs (ID1 and ID2). ID1 and ID2 may be different. So it is structured as:
Map<String,Map<String,String>> ===> <someValue, <ID1,ID2>>
What I am trying to achieve is to get the entire list of the id pairs (ID1 and ID2) for every someValue. So I can store them in a database to keep track of aeronautical information.
Having an map of maps:
Map<String, Map<String, String>> map
you get the inner map simply by calling get
String key = ...; // TODO
Map<String, String> innerMap = map.get(key);
Update to your edit:
It further seems you need something like a map of pairs:
Map<String, Pair<String, String>> mapOfPairs.
Unfortuneatly java has no Pair class.
So write one yourself:
public class Pair {
String id1;
String id2;
}
and have a
Map<String, Pair> mapOfPairs;

save and access data from a map inside of another map

I have a problem saving and accessing data from a map which is inside another map.
Map<String, Integer> Mapname=new LinkedHashMap<String, Integer>();
Mapname.put("hello",2);
Mapname.put("bye",3);
Map<String, Map<String,Integer> OutsiderMap
= new LinkedHashMap<String, Map<String, Integer>>();
String s="name";
OutsiderMap.put(s,Mapname);
for (Map.Entry<String, Map<String,Integer>> entry : OutsiderMap.entrySet())
{
Map<String,Integer> innermap;
innermap=entry.getValue();
for (Map.Entry<String, Integer> entry2 : innermap.entrySet())
{
}
}
Output should be:
Should print the Outsider map not the inner map
Output now:
s={};
My output right now is the name of the string s={}. So my oustider map is empty.
Can you please tell me what is wrong?
Its working with Eclipse Luna, just add the following statement in inner for loop System.out.println(entry2); to access Map elements stored in the an another Map.

Recursively Create Hasmaps in Java

I'm trying to create a new HashMap for each document I have as input. In pseudeocode I can think of something like:
For(eachInputDoc)
{
Map<String, String> mapInputNumber = new HashMap<String, String>;
}
So that for 4 documents you would have:
mapInput1
mapInput2
mapInput3
mapInput4
How can I accomplish this?
It looks like you're trying to declare variables dynamically. You can't do that in Java - the variables themselves are determined at compile time. However, you could create a list:
List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
for (Document doc : docs)
{
Map<String, String> map = new HashMap<String, String>();
// Populate map from doc
maps.add(map);
}
I suggest you make an ArrayList of HashMaps.
You cannot dynamically generate names like mapInput1, mapInput2, etc in Java. You need to think of array or List. Also your problem is not recursive.
I'd do something like this:
Map<MyDocClass, Map<String, String>> myDocData = new HashMapMap<MyDocClass, Map<String, String>>();
for(MyDocClass doc : myDocs) {
Map<String, String> data = new HashMap<String, String>();
// populate the data
myDocData.put(doc, data);
}
Then you can easily access the data for each doc by doing
Map<String, String> data = myDocData.get(doc);
If you know/want to reference the name of the document, you could even use a HashMap of HashMaps.
I would have another map to hold the mapInputs something like this:
Map<Integer,Map<String,String>> context = new HashMap<Integer,Map<String,String>>();
for each(inputDoc)
{
Map<String, String> mapInput = new HashMap<String, String>();
context.put(index,mapInput);
}
U have the aproach of having a List(array,linked) instead of MAP, but this depends of how you`re gonna access that inputMaps! I would say that using a ArrayList is a good one too!
You need to put your hash maps into another (dynamic) container like ArrayList or other HashMap.

Categories

Resources