3

My json file looks like this [actually it has more, I am just putting 2 blocks for example]

[{
        "answerValue": "2021-02-01",
        "parentId": "Policy",
        "instance": 1,
        "fieldId": "PolicyEffectiveDate"
    },
    {
        "answerValue": "2012",
        "parentId": "Insured",
        "instance": 1,
        "fieldId": "DateBusinessStarted"
    }
]

I want to store them in a HashMap and print them.

public void MapCheck() {

        Map<String, Object> dataMap = new HashMap<>();
        List<Map> lstMap = new ArrayList<>();


        dataMap.put("answerValue:", "2021-02-01");
        dataMap.put("parentId:", "Policy");
        dataMap.put("instance:", 1);
        dataMap.put("fieldId:", "PolicyEffectiveDate");
        lstMap.add(dataMap);

        dataMap.put("answerValue:", "Assurestart LLC");
        dataMap.put("parentId:", "Insured");
        dataMap.put("instance:", 1);
        dataMap.put("fieldId:", "Business_Name");
        lstMap.add(dataMap);

        System.out.println(lstMap);
    }


    public static void main(String[] args) {
        Test t = new Test();

        t.MapCheck();
    }
}

Expected: I wanted it to print

[{parentId:=Policy, fieldId:=PolicyEffectiveDate, answerValue:=2021-02-01, instance:=1}, {parentId:=Insured, fieldId:=Business_Name, answerValue:=Assurestart LLC, instance:=1}]

Actual: It is printing, the last value twice.

[{parentId:=Insured, fieldId:=Business_Name, answerValue:=Assurestart LLC, instance:=1}, {parentId:=Insured, fieldId:=Business_Name, answerValue:=Assurestart LLC, instance:=1}]

How can I make it print 2 different values? Thanks in advance for your time and ideas.

1
  • You're putting two references to the same hash map in your list. Just add dataMap = new HashMap<>(); after the first call to add, so that you create a new map for the second element. Commented Jan 31, 2021 at 7:17

3 Answers 3

5

You should create a new map for the second entry instead of overwriting the first entry’s values. Add

dataMap = new HashMap<>();

After adding the first entry to the list.

Sign up to request clarification or add additional context in comments.

Comments

3

You should create a new map for the second map in the list:

    Map<String, Object> dataMap = new HashMap<>();
    List<Map<String, Object>> lstMap = new ArrayList<>();


    dataMap.put("answerValue:", "2021-02-01");
    dataMap.put("parentId:", "Policy");
    dataMap.put("instance:", 1);
    dataMap.put("fieldId:", "PolicyEffectiveDate");
    lstMap.add(dataMap);

    dataMap = new HashMap<>(); // create a new map!
    dataMap.put("answerValue:", "Assurestart LLC");
    dataMap.put("parentId:", "Insured");
    dataMap.put("instance:", 1);
    dataMap.put("fieldId:", "Business_Name");
    lstMap.add(dataMap);

That said, if you actually want to generate JSON, or read a JSON file, I recommend using a JSON serialisation/deserialisation library, such as GSON. That way, you can represent your data not as hash maps, but a class like this:

class MyObject {
    private String answerValue;
    private String parentId;
    private int instance;
    private String fieldId;

    // getters & setters...
}

Comments

1

HashMap as you know is a data structure that works based on unique key and value pair property. In the example above when you perform dataMap.put("answerValue:", "2021-02-01"); it saves the value for this key in the HashMap. However when you perform, dataMap.put("answerValue:", "Assurestart LLC"); the second time, it will override the value of "answerValue:" key as it already exists there.

A better approach is to create a new class that can contain all this data in it and then you can decide on a unique key to store this data in. Thus your values will be an object that contains this entire block of data. For example,

public class MyData {
private String answerValue;
private String parentId;
private Integer instance;
private String fieldId;

//Setters and getters
...

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.