What is the purpose of Guava’s Joiner class?
A) To concatenate elements of a collection into a single string
B) To merge multiple files into one
C) To combine multiple databases
D) To create complex SQL queries
Answer:
A) To concatenate elements of a collection into a single string
Explanation:
Guava’s Joiner
class is used to concatenate elements of a collection into a single string. It provides a flexible way to join the elements of an Iterable
, array, or other collection types with a specified separator.
For example:
Joiner joiner = Joiner.on(", ");
String result = joiner.join(Arrays.asList("one", "two", "three"));
This code joins the elements “one”, “two”, and “three” with a comma and space separator, resulting in the string “one, two, three”. The Joiner
class makes it easy to handle nulls and customize the joining process.