简体   繁体   中英

Add key value pair to List<Map<String, Object>>

I have a List<Map<String, Object>> that returns output as below.

[{ID:55, Item=6455, Quantity=3, Cost=150$},{ID:89, Item=0566, Quantity=2, Cost=30$},{ID:112, Item=5477, Quantity=1, Cost=50$},{ID:345, Item=6768, Quantity=10, Cost=280$}]

I'm trying to add key value pair "Size=large" at beginning of each Map<String, Object> of List. I used below code to add the key and value.

List<Map<String, Object> returnList = null;
returnList = jdbcTemplate.query(itemQuery, extractor);
Map<String,Object> map = new LinkedHashMap<>();
map.put("Size","large");
returnList.add(map);
System.out.println(returnList);

Right now I'm getting output as:

[{ID:55, Item=6455, Quantity=3, Cost=150$},{ID:89, Item=0566, Quantity=2, Cost=30$},{ID:112, Item=5477, Quantity=1, Cost=50$},{ID:345, Item=6768, Quantity=10, Cost=280$}, {Size=large}]

How can I get below output to add value for each Map of the list?

[{Size=large, ID:55, Item=6455, Quantity=3, Cost=150$},{Size=large, ID:89, Item=0566, Quantity=2, Cost=30$},{Size=large, ID:112, Item=5477, Quantity=1, Cost=50$},{Size=large, ID:345, Item=6768, Quantity=10, Cost=280$}]

// I was able to get answer for my question based on #Quadslab reply as below//

int size=returnList.size();
for(Map<String,Object> map : returnList) {
    Map<String,Object> mapcopy = new HashMap<String, Object>();
for(Map<String,Object> entry: map.entrySet()){
mapcopy.put(entry.grtKey(),entry.getValue());
}
    map.clear();
    map.put("Size","large");
    map.putAll(mapcopy);
}
for(Map<String,Object> map : returnList) {
    map.put("Size","large");
}

This adds it to the end.
To add it at the beginning, there is a bit more work.

int size=returnList.size();
for(Map<String,Object> map : returnList) {
    Map<String,Object> mapcopy = new LinkedHashMap<String,Object>(map);
    map.clear();
    map.put("Size","large");
    map.putAll(mapcopy);
}

This code is basically from this answer .

Update the existing map, do not instantiate

Do not instantiate a new Map object, no need for your new LinkedHashMap<>() . You want to update your existing map, not replace the map so append an additional map.

You could use a for-each loop to modify each of the map objects in your list.

for( Map<String, Object> map , listOfMaps )
{
    map.put( "Size" , "large" ) ;
}

Map order

You said:

at beginning of each Map<String, Object>

Your underlying implementation of Map may or may not support an order. To quote the Javadoc:

The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

Use a class

Your code is screaming out for a custom class. Java is a powerful object-oriented language, so use objects to represent this kind of data.

record

Java 16 brings a new feature, records , to more briefly define a class whose main purpose is to communicate data transparently and immutably. You simply declare the member fields. The compiler implicitly creates the constructor, getters, equals & hashCode , and toString .

public record LineItem (  String size , int id , String item , int quantity , BigDecimal cost ) {}

Use like any class.

list.add(
    new LineItem( "large" , 55 , "6455" , 3 , new BigDecimal( 150 ) ) ;
);

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