How can Java convert a list to a string?
In Java, you can use the String.join() method to convert a List to a string. Here is an example code:
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> list = List.of("Hello", "World", "Java");
String str = String.join(",", list);
System.out.println(str);
}
}
In the code above, we first create a List object called list, which contains several string elements. Then, we use the String.join() method to convert the List to a string, specifying a delimiter (using a comma here). Finally, we print out the result.
The output is: Hello, World, Java