The above statement uses join function within the String class to join the elements of the collection into the String. The elements are joint using the specified delimiter which is a “,” in this case.
final List<String> collection = new ArrayList<>();
final String joinedString = String.join(",", collection);
The above examples joins an array list collection of Strings to create a single comma delimited string.
Integer Collection to String
The String.join function only works on String collections. However if you do happen to have a collection of Integers and need to join these integers into a single string delimited by comma, the below example shows how.
final List<Integer> intList = new ArrayList<>();
final String joinedIntString = String.join(",", intList.stream().map(x -> "" + x).collect(Collectors.toList()));
The .map function maps each Integer element into a String element simply by appending the Integer to an empty string.
The above methods simply iterate through all the first level keys within a JSONObject and print them to the console. The .keys() function returns an Iterator and does not support parallel execution. The .keySet() function returns a Set of the keys and maybe integrated in a single thread or in parallel.
If printing to console was the only objective, the lambda expression can be better used as
List<Integer> list1 = new ArrayList<>(Arrays.asList(1,5,10,30,4,8,11));
List<Integer> list2 = new ArrayList<>(Arrays.asList(1,5,10,30,4,8,11));
/* Retains all elements in list that have a value > 5 */
CollectionUtil.retainIf(list1, element -> element > 5);
/* Removes all elements in list that have a value > 5 */
CollectionUtil.removeIf(list2, element -> element > 5);