1

Is there anyway to have a map defined as Map<String, String> that can be added to an array list defined as List<Map<String, String> that has the same key? Ideally we would have an arraylist of maps

[{firstName=john}, {firstName=Kelly}, {firstName=Jack}]

The reason I need the same key is for react.js ui I will need to map over these items and I need to have the same key in each object so I can call

List.map(key, index) = > key.firstName

To build the ui component. Is this possible?

Map<String,String> names = new HashMap<>();

List<Map<String,String> firstNamesLastNamesList = new ArrayList<>();

while(resultSet.next()){
names.put("firstName", resultSet.getString("firstName");
names.put("lastName", resultSet.getString("lastName");
firstNamesLastNamesList.add(names); 
}

The values to populate each map will come from a database. Currently when I add each object to the List of maps as you might imagine, the previous map is overwritten due to the same key of firstName. I am left with a List like [{firstName=Jack, lastName=Hammer}, {firstName=Jack, lastName=Hammer}, {firstName=Jack, lastName=Hammer}] not [{firstName=John, lastName=Carpenter}, {firstName=Kelly, lastName=Johnson}, {firstName=Jack, lastName=Black}] which is the desired out put. Any help would be greatly appreciated.

3
  • share the code that overwrites values for same key Commented Oct 21, 2022 at 3:22
  • The key/value mappings will overwrite, if you give a new key/value with the same key, within a Map. That's just how they work. But adding those maps to a list should be independent of that. Commented Oct 21, 2022 at 3:25
  • this is because you have the object created before the loop Commented Oct 21, 2022 at 3:39

2 Answers 2

1

Your code is doing what is expected. the object that you are creating is before the loop

Map<String,String> names = new HashMap<>();

when you create this object you are saying make a new object called names at memory address 0x84927 - some random address.

then you are adding a reference to that object at index i for each item in the list. when you change the object names the list will look for memory address 0x84927 for each index in the firstNamesLastNamesList.

you need to put that piece of code in the while loop

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

2 Comments

right but wouldn't that be a lot of overhead? Each time creating a new object?
@questionsneedanswers there is possibly a more elegant solution, possibly by using the stream() functionality that arrays have. I'd look into that but really you need to have some point in memory for each map
1

You can update your code like this:

Here,

You need to create a new Map for each resultSet inside the loop and then add it to the list. (And as you are creating map outside the loop which is creating a problem.)

List<Map<String,String>> firstNamesLastNamesList = new ArrayList<>();

        while(resultSet.next()){
            Map<String,String> names = new HashMap<>();
            names.put("firstName", resultSet.getString("firstName"));
            names.put("lastName", resultSet.getString("lastName"));
            firstNamesLastNamesList.add(names);
        }

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.