How to iterate over a JSONArray in java 8 How to iterate over a JSONArray in java 8 json json

How to iterate over a JSONArray in java 8


This is equivalent of or code in Java 8 stream API. Not 100% equivalent, but you can get the main idea.

private static final String COMMITS_IN_PATCH_IDENTIFIER = "patchInformation_svnRevisionpublic";  //key used to identify the commits in a patch from JSON response received from PMTprivate static final String KEY_STRING = "name";private static final String VALUE_STRING = "value";public List<String> getCommitIds (JSONArray array) {     return arrayToStream(array)            .map(JSONObject.class::cast)            .filter(o -> o.get(KEY_STRING).equals(COMMITS_IN_PATCH_IDENTIFIER))            .findFirst()            .map(o -> (JSONArray) o.get(VALUE_STRING))            .map(Main::arrayToStream)            .map(commits ->                    commits.map(Object::toString)                            .map(String::trim)                            .collect(Collectors.toList())            )            .orElseGet(Collections::emptyList);}@Nonnullprivate static Stream<Object> arrayToStream(JSONArray array) {    return StreamSupport.stream(array.spliterator(), false);}