我试图通过streams
和Lambdas
将JSONArray
转换为Map<String,String>
。以下是不工作:Java-8 JSONArray到HashMap
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"));
避免Line 4
类型转换,我做Line 3
Line 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)
而且Line 4
提供了以下错误:
Type mismatch: cannot convert from Object to HashMap<String,String>
我在努力学习兰姆达斯和溪流。有人可以帮我吗?
我不认为这是必然一个副本。这是转换JSONArray,并且问题产生于使用lambda表达式 –
@ozOli在您认为错误的东西downvote或作出反应之前进行两次READ/THINK两次 –