-1

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

Community
  • 1
  • 1
korunos
  • 768
  • 3
  • 11
  • 31
  • Do you want a contiguous hash map made out of all the other hash maps within the array list? Do you want to overwrite previous values, ignore if already exist or throw some exceptions? You will need to provide some more context as to what you are trying to do and where you are failing. – npinti Aug 04 '15 at 07:32
  • I edited my question, so you can see what I want – korunos Aug 04 '15 at 07:35
  • 1
    upvoted, cause all that downvotes make me sad. – Gewure Dec 20 '16 at 09:39

4 Answers4

8

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);
}
Community
  • 1
  • 1
RNJ
  • 15,272
  • 18
  • 86
  • 131
  • I have keys named job_id and there are stored the filenames as values. Thats all – korunos Aug 04 '15 at 07:37
  • if you have lots of keys named job_id then you will overwrite the keys when you add them to a hashmap. Is multimap what you are looking for? – RNJ Aug 04 '15 at 07:38
  • I've put an example how the hashmap inside the arraylist looks like – korunos Aug 04 '15 at 07:42
  • In this case your new hashmap will only have one value. I think you might need a multimap. I'll update the answer – RNJ Aug 04 '15 at 07:44
  • Can I compare this with another HashMap? Because this is what I want to do as next. – korunos Aug 04 '15 at 07:50
3

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);
}
Michal Krasny
  • 5,434
  • 7
  • 36
  • 64
3

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.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

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.

Sandeep Bhardwaj
  • 1,310
  • 1
  • 18
  • 24