0

I have data that is currently being stored in a HashMap<String, ArrayList<HashMap<String, String>>>

I am looking to get a certain value of the ArrayList<HashMap<String, String>>> where the Key is of value "FieldName1". Alternatively, if I try to get the ArrayList Index would it return me both the Key,value pair?

Ultimately what I am trying to do is compare this specific value where the key is "fieldName1" to the other "fieldnName1"'s within the arrayList. If that is the case, should I take the index position of the ArrayList instead?

Thanks!

edit*** The reason HashMap<String, ArrayList<HashMap<String, String>>> is used is because what I am trying to do is process an excel file. I read each row in my excel file and based off the first cell in the row (ex A1) it maps to a certain DB Table. I then have a layoutFile that stores the DB Table name and its corresponding field names. Each new row in that excel file is a different field name.

scrayon
  • 491
  • 4
  • 14
  • 29
  • It depends on what you put in your Map in your List in your Map. Why are you using a Map of List of Maps? – Elliott Frisch Jul 09 '14 at 13:17
  • Why not `Map>> coll = new HashMap>>()`? It's much clearer and not interface-dependent. – EpicPandaForce Jul 09 '14 at 13:18
  • 1
    `HashMap>>` is a sign of a very, **very** bad design. – Maroun Jul 09 '14 at 13:18
  • I will add more context in my question as to why HashMap>> – scrayon Jul 09 '14 at 13:25
  • The reason `HashMap>>` is used is because what I am trying to do is process an excel file. I read each row in my excel file and based off the first cell in the row (ex A1) it maps to a certain DB Table. I then have a layoutFile that stores the DB Table name and its corresponding field names. Each new row in that excel file is a different field name. – scrayon Jul 09 '14 at 13:30
  • Based on your edit, have you considered using a third-party library like Sourceforge's JXL or Apache's POI? http://stackoverflow.com/questions/14980717/what-is-the-better-api-to-reading-excel-sheets-in-java-jxl-or-apache-poi – cobaltduck Jul 09 '14 at 13:36
  • well, I already have all the data stored (already reading the excel file), I am just looking to see how I can retrieve specific values within my ArrayList hashMap – scrayon Jul 09 '14 at 13:40

2 Answers2

0

Alternatively, if I try to get the ArrayList Index would it return me both the Key,value pair?

This will return the hahsmap object and no the key value pair.

Ultimately what I am trying to do is compare this specific value where the key is "fieldName1" to the other "fieldnName1"'s within the arrayList.

Dont know what you meant by this but in a hashmap you cannot have duplicate keys. So you cannot have two "FieldName1" inside the hashmap.

To get the value of "FieldName1" you to traverse the initial hashmap to get the arraylist which contains the hashmap that has the required field. So first get the required arraylist from the hashmap, from the arraylist get the required hashmap and from the hashmap you can get the appropriate field.

Anirudh
  • 55
  • 1
  • 8
  • What I meant is that I want to compare fieldName1 of one hashmap's to another filedName1 of another hashMap. I have setup an arrayList of Hashmap's so I want to compare say arrayList[0] to arrayList[1] – scrayon Jul 09 '14 at 13:33
  • You can get the Hashmaps say like `HashMap temp1 = arraylist.get(0)` & `HashMap temp2 = arraylist.get(1)` . You an then get the values from the individual hashmaps `temp1.get("fieldName1")` & `temp2.get("fieldname2")` and compare them. – Anirudh Jul 10 '14 at 14:35
0

I would say you can use the index of the ArrayList, which will return the HashMap object, and then compare the key and value of the one HashMap with the other one's key and value.

instanceof
  • 1,404
  • 23
  • 30