I have a JSONArray (org.json) like this:
[
{ \"a\": \"a\" },
{ \"b\": \"a\" },
{ \"c\": \"a\" },
{ \"d\": \"a\" },
{ \"e\": \"a\" },
{ \"
If you're seeking a functional style solution, you can wrap the Iterator
into a Stream
and then do whatever you want.
One of the functional solutions:
JSONArray newJsonArray =
StreamSupport.stream(jsonArray.spliterator(), false)
.filter(JSONObject.class::isInstance)
.map(JSONObject.class::cast)
.filter(j -> j.has("a"))
.collect(collectingAndThen(toList(), JSONArray::new));
Note: The solution above does not modify the original JSONArray
. Following the principles of functional programming one should prefer collecting into a new object rather than modifying the existing one.