1. Introduction
Joining is a common operation in programming where you concatenate the elements of a list with a delimiter. In Java, String.join() method can be used to join elements of a List. This method is available since Java 8 and is a convenient utility for this purpose.
2. Program Steps
1. Create a List of String.
2. Use the String.join() method to join the elements of the list with a delimiter.
3. Print the resulting String to the console.
3. Code Program
import java.util.Arrays;
import java.util.List;
public class ListJoinExample {
public static void main(String[] args) {
// Step 1: Create a List of String
List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry", "Date");
// Step 2: Use the String.join() method to join the elements of the list with a delimiter
String result = String.join(", ", fruits);
// Step 3: Print the resulting String to the console
System.out.println(result);
}
}
Output:
Apple, Banana, Cherry, Date
4. Step By Step Explanation
Step 1: We create a List of String containing names of fruits.
Step 2: We use the String.join() method to concatenate the elements of the list, separating each element with a comma and a space.
Step 3: The resulting String is printed to the console, showing the elements of the list joined with the specified delimiter.