我正在尝试迭代List<Map<String, Object>>并想要检查代码是否“已批准” - 如果代码具有“已批准”的值,那么我想在另一个 hashMap中添加“id”作为键和“日期”作为值。
List<Map<String, Object>> prodIds = ((List<Map<String, Object>>) myIds.get("result"));
此 prodIds 返回以下记录集:
[{id=[14766724], Date=[1999-01-01]}, {id=[49295837], code=[approved], Date=[2003-04-01]}]
[{id=[58761474621], code=[approved], Date=[2017-09-30]}, {id=[3368781], code=[Cancelled], Date=[2014-01-01]}, {id=[48843224], code=[Cancelled], Date=[2009-01-01]}]
我想要这样的输出:如果代码被“批准” - 我的新哈希映射应该具有如下值:
map.put("49295837", "2003-04-01")
map.put("58761474621", "2017-09-30")
Java 代码
List<Map<String, Object>> prodIds = ((List<Map<String, Object>>) myIds.get("result"));
System.out.println("prodIds : " +prodIds );
// [{id=[14766724], Date=[1999-01-01]}, {id=[49295837], code=[approved], Date=[2003-04-01]}]
// [{id=[58761474621], code=[approved], Date=[2017-09-30]}, {id=[3368781], code=[Cancelled], Date=[2014-01-01]}, {id=[48843224], code=[Cancelled], Date=[2009-01-01]}]
Map<String, String> newMap = new HashMap<>();
for (Map<String, Object> map : prodIds) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
System.out.println("Key : " +key);
String value = (String) entry.getValue();
System.out.println(" Value : " +value);
}
}
如果代码值被“批准”到新的哈希映射中,我很难将键(id)和值(日期)动态地放入。如果有人可以帮助我,那将非常有帮助。
提前感谢您的帮助!
谢谢