fbpx

Create String from an array in Java

String.join(",", collection);

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<>();
collection.add("Hello");
collection.add("World");

final String joinedString = String.join(",", collection);

System.out.println(joinedString);

The above examples joins an array list collection of Strings to create a single comma delimited string.

Output

Hello,World

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<>();
intList.add(10);
intList.add(20);
intList.add(30);

final String joinedIntString = String.join(",", intList.stream().map(x -> "" + x).collect(Collectors.toList()));

System.out.println(joinedIntString);

Output

10,20,30

The .map function maps each Integer element into a String element simply by appending the Integer to an empty string.

 

Iterate Over Keys of JSONObject – Java

JSON’s are commonly used in many Java programs, with most common libraries being org.json and com.google.gson. The examples below show how to iterate over keys of both json objects in Java 8.

With com.json.JSONObject

jsonObject.keys().forEachRemaining(key -> System.out.println(key));
jsonObject.keySet().forEach(key -> System.out.println(key));
jsonObject.keySet().parallelStream().forEach(key -> System.out.println(key));

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

jsonObject.keySet().forEach(System.out::println);

With com.google.gson.JsonObject

jsonObject.entrySet().forEach(entry -> System.out.println(entry.getKey()));
jsonObject.entrySet().parallelStream().forEach(entry -> System.out.println(entry.getKey()));

The Google’s JsonObject implementation provides for iteration over the key-value pairs inside the JsonObject. So it is not just the key, but the value also can be obtained using a single iteration.

jsonObject.entrySet().parallelStream().forEach(entry -> {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue().getAsString());
});

retainIf and removeIf for Java Collections

Iterate over Java collection classes to conditionally retain or remove values from the collection.

CollectionUtil.retainIf(list, element -> element > 5);

Retains all elements in the collection that have a value greater than 5.


CollectionUtil.removeIf(list, element -> element > 5);

Removes all elements from the list that have a value greater than 5.


The CollectionUtil class is available as part of BlobCity java-commons open source distribution of useful Java utilities.

https://github.com/blobcity/java-commons

Jar file: https://github.com/blobcity/java-commons/tree/master/target 

Complete Implementation

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);

System.out.println("List 1");
list1.forEach(System.out::println);

/* Removes all elements in list that have a value > 5 */
CollectionUtil.removeIf(list2, element -> element > 5);

System.out.println("\nList 2");
list2.forEach(System.out::println);

Output

List 1
10
30
8
11

List 2
1
5
4