怎么用Lambda遍历List<Map<String,Object>>得到Map<String,String>?
publicstaticvoidmain(String[]args){List<Map<String,Object>>testList=Lists.newArrayLis...
public static void main(String[] args) {
List<Map<String, Object>> testList = Lists.newArrayList();
Map<String, Object> m1 = Maps.newHashMap();
m1.put("a", "a1");
m1.put("b", "b1");
m1.put("x", "x1");
Map<String, Object> m2 = Maps.newHashMap();
m1.put("a", "a2");
m1.put("b", "b2");
m1.put("x", "x2");
Map<String, Object> m3 = Maps.newHashMap();
m1.put("a", "a3");
m1.put("b", "b3");
m1.put("x", "x3");
testList.add(m1);
testList.add(m2);
testList.add(m3);
// Map<String, String> result = testList.stream().....
// 要求最终结果:{a1=b1,a2=b2,a3=b3}
} 展开
List<Map<String, Object>> testList = Lists.newArrayList();
Map<String, Object> m1 = Maps.newHashMap();
m1.put("a", "a1");
m1.put("b", "b1");
m1.put("x", "x1");
Map<String, Object> m2 = Maps.newHashMap();
m1.put("a", "a2");
m1.put("b", "b2");
m1.put("x", "x2");
Map<String, Object> m3 = Maps.newHashMap();
m1.put("a", "a3");
m1.put("b", "b3");
m1.put("x", "x3");
testList.add(m1);
testList.add(m2);
testList.add(m3);
// Map<String, String> result = testList.stream().....
// 要求最终结果:{a1=b1,a2=b2,a3=b3}
} 展开
2个回答
展开全部
public static void main(String[] args) throws Exception {
List<Map<String, Object>> testList = new ArrayList<>();
Map<String, Object> m1 = new HashMap<>();
m1.put("a", "a1");
m1.put("b", "b1");
m1.put("x", "x1");
Map<String, Object> m2 = new HashMap<>();
m2.put("a", "a2");
m2.put("b", "b2");
m2.put("x", "x2");
Map<String, Object> m3 = new HashMap<>();
m3.put("a", "a3");
m3.put("b", "b3");
m3.put("x", "x3");
testList.add(m1);
testList.add(m2);
testList.add(m3);
Map<String, String> m4 = new HashMap<>();
testList.stream().forEach(m -> {
m4.put((String) m.get("a"), (String) m.get("b"));
});
m4.forEach((x, y) -> {
System.out.println(x + y);
});
System.out.println("end");
}
List<Map<String, Object>> testList = new ArrayList<>();
Map<String, Object> m1 = new HashMap<>();
m1.put("a", "a1");
m1.put("b", "b1");
m1.put("x", "x1");
Map<String, Object> m2 = new HashMap<>();
m2.put("a", "a2");
m2.put("b", "b2");
m2.put("x", "x2");
Map<String, Object> m3 = new HashMap<>();
m3.put("a", "a3");
m3.put("b", "b3");
m3.put("x", "x3");
testList.add(m1);
testList.add(m2);
testList.add(m3);
Map<String, String> m4 = new HashMap<>();
testList.stream().forEach(m -> {
m4.put((String) m.get("a"), (String) m.get("b"));
});
m4.forEach((x, y) -> {
System.out.println(x + y);
});
System.out.println("end");
}
追问
只能这样吗?不能一个链式操作直接返回一个map吗
追答
我觉得是可行,但是我技术也没达到
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询