2

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);
}
Nithya shree
  • 37
  • 2
  • 10

2 Answers2

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

Quadslab
  • 294
  • 3
  • 9
  • Thanks! I am able to add to to the end. Now I need to get it at the beginning. – Nithya shree Jun 06 '21 at 18:16
  • Thanks for providing clone example. map.clone() method didn't work for me but I was able to get output by editing few lines. – Nithya shree Jun 06 '21 at 18:55
  • int size=returnList.size(); for(Map map : returnList) { Map mapcopy = new HashMap(); for(Map entry: map.entrySet()){ mapcopy.put(entry.grtKey(),entry.getValue()); } map.clear(); map.put("Size","large"); map.putAll(mapcopy); } – Nithya shree Jun 06 '21 at 19:02
  • @Nithyashree Thanks for the feedback! I changed my method to use the `LinkedHashMap(Map extends K, ? extends V> m)` Constructor. Also remember that there is an memory overhead to the object creation, so at large scales you should maybe look into other methods (if performance is not good enough), like the answer from Basil Bourque. – Quadslab Jun 06 '21 at 20:03
0

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 ) ) ;
);
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154