Java-8 JSONArray for HashMap

I am trying to convert JSONArrayto Map<String,String>via streamsand Lambdas. The following does not work:

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"));

To avoid type casting in Line 4, I doLine 3

Line 3 gives the following errors:

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)

And it Line 4produces the following error:

Type mismatch: cannot convert from Object to HashMap<String,String>

I am trying to learn lambdas and streams. Can someone help me?

+4
source share
1 answer

It would seem that json-simple JSONArrayextends ArrayListwithout providing any generic types. This results in the streamreturn of a stream, which also has no type.

, List JSONArray

List<Object> jsonarray = new JSONArray();

:

Map<String, String> map = jsonarray.stream().map(Object::toString).collect(Collectors.toMap(s -> s, s -> "value"));
+3

Source: https://habr.com/ru/post/1623281/


All Articles