简体   繁体   中英

Hashmap overwrites values. How to add multiple of the same key?

I know hashMap overwrites the kay, but I really need the same key to be provided for another value. What is also the issue is that in the postRequest further down, it needs to be set as a Map value.

So how can the below be fixed so that the body contains all the field and their values below as displayed in the table?

So we can't have field3 = tree, cone , it has to be field 3 = tree, field 3 = cone or the service will fail.

    Example step:
       |field     |value                                       |
       |----------|--------------------------------------------|
       |field1    |shop                                        |
       |field2    |apple                                       |
       |field3    |tree                                        |
       |field3    |cone                                        |


    @Step("Example step: <table>")
    public void exampleStep(Table table) {
        Map<String, Object> body = new HashMap<>();

           table.getTableRows().forEach(row -> {
            String value = row.getCell(VALUE);
            String field = row.getCell(FIELD);

                body.put(field, value);

        });

final String url = String.format("%s/service/%s", System.getenv(ENDPOINT), service);

new DBrequest(dataStore, url, HttpMethod.POST).postRequest(body);

If you have a Map<String, List<String>> for example, you have to check if keys are present when you are inputting values, see this:

@Step("Example step: <table>")
public void exampleStep(Table table) {
    table.getTableRows().forEach(row -> {
        String value = row.getCell(VALUE);
        String field = row.getCell(FIELD);

        // you have to check if the key is already present
        if (body.containsKey(field)) {
            // if it is, you can simply add the new value to the present ones
            body.get(field).add(value);
        } else {
            // otherwise you have to create a new list
            List<String> values = new ArrayList<>();
            // add the value to it
            values.add(value);
            // and then add the list of values along with the key to the map
            body.put(field, values);
        }
    });
}

You can iterate such a Map in several ways, one is this:

for (Entry<String, List<String>> entry : body.entrySet()) {
    // print the key first
    System.out.println(entry.getKey() + ":");
    // then iterate the value (which is a list)
    for (String value : entry.getValue()) {
        // and print each value of that list
        System.out.println("\t" + value);
        }
    };
}

Please note:
This is a simple example without any content and it doesn't handle any casting from Object .

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