code first:
net.sf.json.JSONArray ja = new net.sf.json.JSONArray();
for (int i = 0; i < 3; i++) {
net.sf.json.JSONObject obj = new net.sf.json.JSONObject();
obj.put("number", "k"+i);
ja.add(obj);
}
System.out.println(ja.stream().map(s->((net.sf.json.JSONObject)s).getString("number")).peek(s->System.out.println(s.getClass())).collect(Collectors.joining(";")));
as you can see, I try to get a String of JSONObject and join them. And it works above like this:
class java.lang.String
class java.lang.String
class java.lang.String
k0;k1;k2
However ja.stream().map(s->((JSONObject)s).getString("number")) seems return not a Stream<String> but Stream<Object> which I couldn't append String intermidiate operation like .map(s->s.substring(3)). In other words, type lost.
So can anybody give any advice?
JSONArrayimplements raw types. It'll poison your whole stream. Use a genericListinstead.