gpt4 book ai didi

java - 将 List> 转换为 Map

转载 作者:行者123 更新时间:2023-11-29 05:51:18 25 4
gpt4 key购买 nike

我正在使用 queryForList从我的数据库中获取一个表,它给了我 List<Map<String, Object>> .我想使用我选择的两列将其转换为 Map<String, Integer> .

目前我正在做这件事

List<Map<String, Object>> customers = jdbc.queryForList("SELECT id, name FROM customers");

Map<String, Integer> customerMap = new HashMap<String, Integer>();

for (Map<String, Object> each : customers)
{
String name = ((String)each.get("name")).trim();
Integer id = Integer.valueOf(((BigDecimal)each.get("id")).intValue());

customerMap.put(name, id);
}

并想知道是否有更好的方法。谢谢

最佳答案

这太晚了,但只是通过搜索偶然发现了这个,发现也许可以分享我的代码:

   private Map<String, Integer> convertMap(List<Map<String, Object>> input) {
logger.trace("convertMap");
Map<String, Integer> dest = new HashMap<>();
for (Map<String, Object> next : input) {
for (Map.Entry<String, Object> entry : next.entrySet()) {
dest.put(entry.getKey(), (Integer) entry.getValue());
}
}
return dest;
}

关于java - 将 List<Map<String, Object>> 转换为 Map<String, Integer>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13762321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com