简体   繁体   中英

How to use HashMap when Json file has same key value

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.

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.

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...
}

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
...

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM