Following code should work
Sample code to initialize the JSONArray
JSONArray jsonArray =new JSONArray();
JSONObject j1 = new JSONObject();
j1.put ("key", "Stack");
JSONObject j2 = new JSONObject();
j2.put ("key", "Overflow");
jsonArray.add(j1);
jsonArray.add(j2);
Convert the JSONArray to List. There were couple of issues in your code. The first one being, you were using JSONArray inside your map function and it should be JSONObject . The second one is, map returns Stream and we need to typecast it to Stream<String>
List<String> output = arrayToStream(jsonArray)
.map(JSONObject.class::cast)
.map(s -> s.get("key").toString())
.collect(Collectors.toList());
Util method to convert JSONArray to Stream<Object>
private static Stream<Object> arrayToStream(JSONArray array) {
return StreamSupport.stream(array.spliterator(), false);
}