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.
Related
This question already has answers here:
Java - Initialize a HashMap of HashMaps
(4 answers)
Closed 4 years ago.
How can I create a "multidimensional" HashMap with HashMaps as Value without initializing every HashMap like you see below?
HashMap<Integer, String> DenmarkBasic = new HashMap<Integer, String>();
DenmarkBasic.put(1, "http://website1.com/");
DenmarkBasic.put(2, "http://website2.com/");
HashMap<Integer, String> DenmarkMisc = new HashMap<Integer, String>();
DenmarkMisc.put(1, "http://website1.com/");
DenmarkMisc.put(2, "http://website2.com/");
HashMap<String, HashMap<Integer, String>> DenmarkPanel = new HashMap<String, HashMap<Integer, String>>();
DenmarkPanel.put("Basic", DenmarkBasic);
DenmarkPanel.put("Misc", DenmarkMisc);
HashMap<String, HashMap<String, HashMap<Integer, String>>> NordicCountry = new HashMap<String, HashMap<String, HashMap<Integer, String>>>();
NordicCountry.put("Denmark", DenmarkPanel);
NordicCountry.put("Sweden", SwedenPanel);
HashMap<String, HashMap<String, HashMap<String, HashMap<Integer, String>>>> Market = new HashMap<String, HashMap<String, HashMap<String, HashMap<Integer, String>>>>();
Market.put("Nordic", NordicCountry);
I just want to use a loop, because it would be too much Maps.
Don't do that!
Nesting hash maps makes your code very very complicated very very quickly. Just look at how long your type names are getting.
You should write the data in another format, like JSON, and then parse it.
Your JSON would look something like this:
{
"Market" : {
"Nordic": {
"Denmark": {
"Basic": ["website1.com", "website2.com"],
"Misc": ["website1.com", "website2.com"]
},
"Sweden": {
"Basic": ["website1.com", "website2.com"],
"Misc": ["website1.com", "website2.com"]
},
}
}
}
And then you use a JSON parser to parse it. For example, as shown in this answer, you can use org.json. To get the list of basic Denmark websites:
jsonObject
.getJSONObject("Market")
.getJSONObject("Nordic")
.getJSONObject("Denmark")
.getJSONArray("Basic")
There are also other libraries mentioned in that post. Find the one you like the most and use it!
It's generally a bad practice to create such nested structures (like map contains a map, or list containing maps, etc.), so Guava comes with new collections, you can find there things like multimap, multiset which will help you write better, safer and future-proof code. Try it out, such structures are availalbe in Guava and Apache Collections
https://github.com/google/guava/wiki/NewCollectionTypesExplained#multimap
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!
I have this ArrayList
public ArrayList<HashMap<String, String>> xmlFileNames = new ArrayList<>();
and I want to convert this to:
HashMap<String, String> comparemap2 = new HashMap<>();
What I want is: I want all the Items inside the ArrayList and want to put them into the HashMap
My HashMap looks like:
KEY VALUE
job_id 032014091029309130921.xml
job_id 201302149014021492929.xml
job_id 203921904901920952099.xml
EDIT:
Later I want to compare this map with an existing map:
Properties properties = new Properties();
try {
properties.load(openFileInput("comparexml.kx_todo"));
} catch (IOException e) {
e.printStackTrace();
}
for (String key : properties.stringPropertyNames()) {
compareMap.put(key, properties.get(key).toString());
}
HashMap<String, String> oldCompareMap = new HashMap<>();
for (HashMap key : xmlFileNames) {
oldCompareMap.putAll(key);
}
isEqualMaps(oldCompareMap, compareMap);
I only want to compare, if the filename exists in the compareMap. If not, than add it to the xmlFileName Map
I've looked up in StackOverFlow, how I can convert ArrayList to HashMap. But the other Threads treat data types like Item or Product.
I hope you can help me!
Kind Regards
Given...
public ArrayList<HashMap<String, String>> xmlFileNames = new ArrayList<>();
then something like this should do it.
HashMap<String, String> nhm = new HashMap<>();
for (HashMap xmlFileHm : xmlFileNames ) {
nhm.putAll(xmlFileHm);
}
but be aware if you have duplicate keys in your hashmaps they will get overwritten.
You should also think about coding to interfaces. Take a look at Map and List rather than typing your collections to implementations (ArrayList and HashMap). Take a look at this thread which is quite interesting What does it mean to "program to an interface"?
Depending on what you are trying to do as well you might consider a MultiMap as this might server your purposes better
Edit After update to the question...
A multimap would be better here with one key and multiple values. Although arguably if the key never changes then you could just store the values in a list. For multiamps you can use Google's guava library or do one yourself. For example (not checked for compilation errors as Im doing this from my head)
Map<String, List<String>> m = new HashMap<>();
if (m.containsKey("key")) {
m.get("key").add("new value");
}
else {
List<String> l = new ArrayList<>();
l.add("new value");
m.put("key", l);
}
You can create a new HashMap, then iterate through the list and put all elements from the map from the list to the main map.
List<Map<String, String>> list = new ArrayList<>();
Map<String, String> map = new HashMap<>();
for (Map<String, String> mapFromList : list) {
map.putAll(mapFromList);
}
You can try something like this..
ArrayList<HashMap<String, String>> xmlFileNames = new ArrayList<>();
HashMap<String, String> comparemap2 = new HashMap<>();
for(HashMap<String, String> i:xmlFileNames){
comparemap2.putAll(i);
}
You may need to consider the case of duplicate keys. else they will get override.
Create a new map and put All each element of arrayList to the map.
But in that case if you have same keys in two element of arrayList (hashmap) then it will override the previous one.
I have got some troubles converting each value in my HashMap to a String.
private static HashMap<String, List<Music>> musiksammlung = new
HashMap<String, List<Music>>();
This is my constructor for the HashMap. The key represents the album, the value a list of tracks from this album.
Now I want to convert each Music object to a String without creating a new HashMap, is this
possible?
I've tried it with the Iterator scheme, for loop over the entry set and so on but nothing seems to work.
Edit://
My code for the convertmethod:
public HashMap<String, List<String>> generateFormatList() {
HashMap<String, List<String>> formatList = new HashMap<String, List<String>>();
for(String key : musiksammlung.keySet())
formatList.put(key, musiksammlung.get(key).toString());
return musiksammlung;
}
But this always results in an error "is not applicable for the Arguments (String, String) so I have no idea. Do I have to override toString()?
You're on the right path but you need to convert the existing List<Music> to a List<String> and put the List<String> into your new HashMap.
You also then want to return your newly created HashMap<String, List<String>> instead of your original one.
public HashMap<String, List<String>> generateFormatList() {
HashMap<String, List<String>> formatList = new HashMap<String, List<String>>();
for(String key : musiksammlung.keySet()) {
// Value to store in map
List<String> value = new ArrayList<String>();
// Get the List<Music>
List<Music> musicList = musiksammlung.get(key);
for (Music m: musicList) {
// Add String of each Music object to the List
value.add(m.toString);
}
// Add the value to your new map
formatList.put(key, value);
}
// Return the new map
return formatList;
}
So answer your question:
Now I want to convert each Music object to a String without creating a
new HashMap, is this possible?
You need to create a new HashMap, because it's storing different type of value: List<Music> is different from List<String>.
Also as mentioned in my previous answer, make sure you override Music.toString() so that it returns a meaningful String for you instead of the one it inherits from its parent classes, which includes at least java.lang.Object
formatList wants a List<String>, but musiksammlung.get(key).toString() returns a String (not a List<String>). Did you mean this?
HashMap<String, String> formatList = new HashMap<String, String>();
Have you tried something like this:
Iterator<String> it = musiksammlung.keySet().iterator();
while (it.hasNext()) {
List<Music> ml = musiksammlung.get(it.next());
for (Music m : ml)
System.out.println(m.toString());
}
And of course you should override the Music#toString() method with something you could use.
Try to change your HashMap like this:
private static HashMap<String, List<Object>> musiksammlung = new HashMap<String,List<Object>>();
So you can save any kind of objects in this HashMap. Also use instanceof to check the type of the object before using it.
I am trying to create a dictionnary in a <K, List<V>> format.
private static Map<String, Collection<String>> dict = new HashMap<String, Collection<String>>();
Using new HashMap<>(); or new HashMap<String, ArrayList<String>>(); throws incompatible data types error
I need a dictionary similar to the one below.
a: apple, ajar, axe, azure
b: ball, bat, box
d: dam, door, dish, drown, deer, dare
u: urn, umbrella
y: yolk
To do this, I worte below code. put() returns incompatible paramters compilation error. What is the right way to use put() for this example?
dict.put("a", "apple");
dict.put("a", "ajar");
.
.
.
dict.put("u", "umbrella");
dict.put("y", "yolk");
You need to place a List as the value to the map, for example:
List<String> listA = Arrays.asList("apple", "ajar", "axe", "azure");
dict.put("a", listA);
Alternatively, you can use guava Multimap which allows more than one value to be mapped to a given key.
This is because you need to put an arrayList in the value as your Map declaration is Map<String, Collection<String>> so it cannot take Map<String, String>.
ArrayList<String> list = new ArrayList<String>();
list.add("apple");
dict.put("a",list );
As per java 7 you can do it using diamond operator so you can create a map as,
List<String, List<String>> = new ArrayList<>();
What you need is this;
List al = new ArrayList<String>();
al.add("apple");
al.add("ajar");
HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
hm.put("a", al);
System.out.println(hm.get("a"));
This is because, when you use;
private static Map<String, Collection<String>>
You need a Collection like a List. DoNOT insert Objects as Strings
You can only follow the definition you have done :
Map<String, Collection<String>> implies you use dict.put(a,b) with a being a String and b a Collection.
You're trying to put a String as a value that's your problem.
You may want to do something like that :
Collection col = dict.get("a");
if (col == null) {
col = new ArrayList();
}
col.add("apple");
dict.put("a",col);
I would first change the type of the dictionary to
private static Map<Character, ArrayList<String>> dict = new HashMap<>();
It'll allow easier putting of array lists as generics are not covariant.
For each letter, create:
ArrayList<String> myList=new ArrayList<>();
and put() it to dict with
dict.put(myList);
Then you can add words with:
dict.get(letter).put(word);
Your exact need is MultiMap feature of apache-commons
MultiMap dict = new MultiHashMap();
dict.put("a", "apple");
dict.put("a", "ajar");
.
.
.
dict.put("u", "umbrella");
dict.put("y", "yolk");