You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is no reason for us to expose the back end data types used and would limit us possibly changing the implementation in the future if a better option presents itself.
Since JSONArray already implements Iterable and assuming you are using Java8+, you can do something like this:
JSONArrayja = newJSONArray("[{\"x\": 1}, {\"x\": 2,\"y\": 2.5}, {\"x\": 3}]");
Stream<Object> myStream = java.util.StreamSupport(ja.spliterator(), false); // true for parallel processing// process the stream how you want.
if you just want to iterate over the JSONArray using oldschool Java7- iterators, you can do that like so:
JSONArrayja = newJSONArray("[{\"x\": 1}, {\"x\": 2,\"y\": 2.5}, {\"x\": 3}]");
for(Objectitem : ja) {
// do something with each item
}
One Caveat with either approach which both use the iterator() method is that it's exposing the raw items stored in the JSONArray.myArrayList (which sounds like what you wanted anyway). But what this means is that if you do any code-built JSONArrays (using the put* methods), you may not get back JSON compliant values as the JSON conversions are only garunteed to happen using the JSONArray.get* methods, which the iterator does not call. (see the put(Object) method).
I understand the objections. I am using old code for this library which has been rewritten previously. Performs multiple operations on "myArrayList" such as sorting. The library was not "Iterable". I want to use the current via Maven.
Thank you for your help. Regards and I close.
Activity
johnjaylward commentedon Mar 21, 2022
There is no reason for us to expose the back end data types used and would limit us possibly changing the implementation in the future if a better option presents itself.
johnjaylward commentedon Mar 21, 2022
What are you trying to accomplish? Perhaps there is a better way to do what you want without exposing the internals?
jacekpodgorz commentedon Mar 21, 2022
Thank you for your answer.
I create Array:
[{" x ": 1}, {" x ": 2," y ": 2.5}, {" x ": 3}]the variable "myArrayList" is "ArrayList ".
If use method "toList()" I get an "ArrayList ".
The variable is garbled. Working on JSONArray using "toList", I will always get a new object.
It would be helpful to download "Stream".
johnjaylward commentedon Mar 21, 2022
Since JSONArray already implements
Iterableand assuming you are using Java8+, you can do something like this:Docs:
if you just want to iterate over the JSONArray using oldschool Java7- iterators, you can do that like so:
One Caveat with either approach which both use the
iterator()method is that it's exposing the raw items stored in theJSONArray.myArrayList(which sounds like what you wanted anyway). But what this means is that if you do any code-built JSONArrays (using theput*methods), you may not get back JSON compliant values as the JSON conversions are only garunteed to happen using theJSONArray.get*methods, which theiteratordoes not call. (see theput(Object)method).jacekpodgorz commentedon Mar 21, 2022
Thank you for your answer.
I understand the objections. I am using old code for this library which has been rewritten previously. Performs multiple operations on "myArrayList" such as sorting. The library was not "Iterable". I want to use the current via Maven.
Thank you for your help. Regards and I close.