添加键值对到列表<Map<String, Object>>
Add key value pair to List<Map<String, Object>>
我有一个 List
[{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$}]
我正在尝试在列表的每个 Map 的开头添加键值对“Size=large”。我使用下面的代码添加键和值。
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);
现在我得到的输出是:
[{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}]
我怎样才能得到下面的输出来为列表中的每个地图增加价值?
[{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$}]
// 根据下面的#Quadslab 回复,我能够得到我的问题的答案//
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");
}
这将它添加到最后。
加在开头,有点功夫
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);
}
这段代码基本来源于此answer.
更新现有地图,不实例化
不要实例化新的 Map
对象,不需要您的 new LinkedHashMap<>()
。您想更新现有地图,而不是替换地图,因此附加一张附加地图。
您可以使用 for-each 循环来修改列表中的每个地图对象。
for( Map<String, Object> map , listOfMaps )
{
map.put( "Size" , "large" ) ;
}
地图顺序
你说:
at beginning of each Map<String, Object>
您的 Map
基础实现可能支持也可能不支持订单。引用 Java 文档:
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.
使用 class
您的代码需要自定义 class。 Java是一种强大的面向对象语言,所以用对象来表示这种数据。
record
Java 16 带来了一个新特性,records,更简单地定义了一个class,其主要目的是透明和不可变地通信数据。您只需声明成员字段。编译器隐式创建构造函数、getter、equals
& hashCode
和 toString
.
public record LineItem ( String size , int id , String item , int quantity , BigDecimal cost ) {}
像任何 class.
一样使用
list.add(
new LineItem( "large" , 55 , "6455" , 3 , new BigDecimal( 150 ) ) ;
);
我有一个 List
[{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$}]
我正在尝试在列表的每个 Map
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);
现在我得到的输出是:
[{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}]
我怎样才能得到下面的输出来为列表中的每个地图增加价值?
[{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$}]
// 根据下面的#Quadslab 回复,我能够得到我的问题的答案//
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");
}
这将它添加到最后。
加在开头,有点功夫
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);
}
这段代码基本来源于此answer.
更新现有地图,不实例化
不要实例化新的 Map
对象,不需要您的 new LinkedHashMap<>()
。您想更新现有地图,而不是替换地图,因此附加一张附加地图。
您可以使用 for-each 循环来修改列表中的每个地图对象。
for( Map<String, Object> map , listOfMaps )
{
map.put( "Size" , "large" ) ;
}
地图顺序
你说:
at beginning of each Map<String, Object>
您的 Map
基础实现可能支持也可能不支持订单。引用 Java 文档:
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.
使用 class
您的代码需要自定义 class。 Java是一种强大的面向对象语言,所以用对象来表示这种数据。
record
Java 16 带来了一个新特性,records,更简单地定义了一个class,其主要目的是透明和不可变地通信数据。您只需声明成员字段。编译器隐式创建构造函数、getter、equals
& hashCode
和 toString
.
public record LineItem ( String size , int id , String item , int quantity , BigDecimal cost ) {}
像任何 class.
一样使用list.add(
new LineItem( "large" , 55 , "6455" , 3 , new BigDecimal( 150 ) ) ;
);