-1

I want to iterate the Arraylist of Arraylist, get the values and insert them into table. My code is working for single Arraylist. What I have achieved for single list is:

private String setreportColLinksBean(List<Map<String, String>> allValuesMap) {
    for (Map<String, String> result: allValuesMap) {
        Map<String, String> map = new HashMap<String, String>();
        if (result.get("it_datatype") != null) {
            map.put("it_datatype", (String) result.get("it_datatype"));
            it_datatype = result.get("it_datatype");
        }
        
        if (result.get("item_value") != null) {
            map.put("item_value", (String) result.get("item_value"));
            item_value = result.get("item_value");
        }
        
        ReportLinks reportlinks = new ReportLinks();
        reportlinks.setItem_datatype(it_datatype);
        reportlinks.setItem_value(item_value);
        saveReportLinks(reportlinks);
    }
}

But I want to iterate data which is like this:

[
  [
   {it_datatype=Character, at_desc=Scholarship Form, it_code=ITM0001, at_bpcode=PR00000122}, 
   {it_datatype=Character, at_desc=Scholarship Form, it_code=ITM0002, at_bpcode=PR00000122}
  ],
  [
   {it_datatype=Character, at_desc=initiate 2, it_code=ITM0001, at_bpcode=PR00000625}, 
   {it_datatype=Character, at_desc=initiate 2, it_code=ITM0002, at_bpcode=PR00000625}
  ]
]

How do I iterate this list and get the values for inserting them into the table. I am very new to Java. Please help. TIA.

flaxel
  • 4,173
  • 4
  • 17
  • 30
abhi25
  • 37
  • 6
  • 2
    What you _should_ be doing is creating a POJO (or multiple nested ones) and deserializing into that instead of unstructured maps and lists. – chrylis -cautiouslyoptimistic- Jun 15 '21 at 20:05
  • @chrylis Could you please share some sample code for reference? I am completely new to this concept of POJO. Thank-you. – abhi25 Jun 15 '21 at 20:14
  • Look into Jackson and Gson to map Java objects to JSON, this might be what you need. You can also look into the serializing feature of Java but that might be more complex depending on what you are trying to do. – Nyamiou The Galeanthrope Jun 15 '21 at 20:26
  • @NyamiouTheGaleanthrope the question is not about mapping to JSON. If anything it is Mapping from JSON. Let's assume that this is the best a mapper can do. No library will save the produced structure for him, he will have to manually iterate over the lists. – Blaž Mrak Jun 15 '21 at 20:32
  • [What is a POJO?](https://stackoverflow.com/questions/1612334/difference-between-dto-vo-pojo-javabeans) It's a _plain old Java object_. – andrewJames Jun 15 '21 at 20:47
  • @BlažMrak *no library will save the produced structure* This is *exactly* what Jackson and Gson do. – chrylis -cautiouslyoptimistic- Jun 15 '21 at 21:47
  • @chrylis-cautiouslyoptimistic- they map, from one class to another, not save to wherever he needs to save... As I understand his question, he needs to iterate over a 2D list to save it, not map it. I'm also assuming that he has to work with this format of data. Adding a mapper would add an unnecessary overhead for this simple function especially because he already has 1D loop working. – Blaž Mrak Jun 15 '21 at 22:01

1 Answers1

-1

Add a for loop around it. The dots are the there for abbreviation.

public void save(List<List<Map<String, String>>> nestedLists) {
  for(var nestedList : nestedLists) {
    setreportColLinksBean(nestedList);
  }
}
Blaž Mrak
  • 244
  • 2
  • 6
  • I tried your provided solution of adding for loop, but it throws an exception. I don't know if I'm doing it correctly or not. Can you please elaborate more? Thank you. – abhi25 Jun 15 '21 at 21:15
  • You have to tell me what exception it is. I just looped over your outer list and called your method with a parameter which you said worked. – Blaž Mrak Jun 15 '21 at 21:43
  • I updated the answer, hope it is clearer. – Blaž Mrak Jun 15 '21 at 21:56
  • I am getting this exception after implementing above code. HibernateSystemException: a different object with the same identifier value was already associated with the session – abhi25 Jun 16 '21 at 05:41
  • This is not the problem with the code above, but with how your save method works. It is basically a problem with the Hibernate and not with the looping. If you delete the line where you call saveReportLinks(), the code will work. If you put System.out.println(reportLinks) you will see that all the links get created. – Blaž Mrak Jun 16 '21 at 09:23