1

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?

1
  • 1
    JSONArray implements raw types. It'll poison your whole stream. Use a generic List instead. Commented Mar 30, 2017 at 7:19

1 Answer 1

3

Breaking down your stream statement does result in:

net.sf.json.JSONArray ja = new net.sf.json.JSONArray();
Stream stream1 = ja.stream();
Stream stream2 = stream1.map(s -> ((JSONObject) s).getString("number"));

As you can see, stream1 is not of a generic type. Hence any transformations will not produce a generic type out of a non generic type. Changing either variable ja or variable stream1 to a generic type will result in a Stream<String> for variable stream2:

net.sf.json.JSONArray ja = new net.sf.json.JSONArray();
Stream<Object> stream1 = ja.stream();
Stream<String> stream2 = stream1.map(s -> ((JSONObject) s).getString("number"));

Hence if you want to preserve type information you need a generic type which may carry it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.