我正在尝试通过streams
和Lambdas
将JSONArray
转换为Map<String,String>
。以下代码不起作用:
org.json.simple.JSONArray jsonArray = new org.json.simple.JSONArray();
jsonArray.add("pankaj");
HashMap<String, String> stringMap = jsonArray.stream().collect(HashMap<String, String>::new, (map,membermsisdn) -> map.put((String)membermsisdn,"Error"), HashMap<String, String>::putAll);
HashMap<String, String> stringMap1 = jsonArray.stream().collect(Collectors.toMap(member -> member, member -> "Error"));
为避免在
第4行
出现类型转换错误,我正在执行第3行
。
第3行
会产生以下错误:Multiple markers at this line
- The type HashMap<String,String> does not define putAll(Object, Object) that is applicable here
- The method put(String, String) is undefined for the type Object
- The method collect(Supplier, BiConsumer, BiConsumer) in the type Stream is not applicable for the arguments (HashMap<String, String>::new, (<no type> map, <no type> membermsisdn)
-> {}, HashMap<String, String>::putAll)
而第四行
会出现以下错误:
Type mismatch: cannot convert from Object to HashMap<String,String>
我正在尝试学习Lambda表达式和流。有人可以帮助我吗?