json - Java-8 JSONArray to HashMap -
i'm trying convert jsonarray
map<string,string>
via streams
, lambdas
. following isn't working:
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 typecasting in line 4
, i'm doing line 3
line 3
gives following errors:
multiple markers @ line - type hashmap<string,string> not define putall(object, object) applicable here - method put(string, string) undefined type object - method collect(supplier, biconsumer, biconsumer) in type stream not applicable arguments (hashmap<string, string>::new, (<no type> map, <no type> membermsisdn) -> {}, hashmap<string, string>::putall)
and line 4
gives following error:
type mismatch: cannot convert object hashmap<string,string>
i'm trying learn lambdas , streams. can me out?
it appear json-simple's jsonarray
extends arraylist
without providing generic types. causes stream
return stream
doesn't have type either.
knowing this, can program on interface of list
instead of jsonarray
list<object> jsonarray = new jsonarray();
doing allow stream so:
map<string, string> map = jsonarray.stream().map(object::tostring).collect(collectors.tomap(s -> s, s -> "value"));
Comments
Post a Comment