Convert List of String> to List of Object java
InJava, we have a lot of ways to convert List of String object to String object with a delimiter between the elements in List object. In this tutorial, I will share with you all some ways as below: The first one, we will use StringBuilder object in Java. We will read all elements in the List, one by one and useStringBuilderto add the delimiter among all elements. Note that, with the first element, we will not add the delimiter! In detail, the method can be written as below: Java
Example: The second one, we will use collect() method of Stream object which was introduced since Java 8. Java
Example: The third one, we will use join() static method of String object. Since Java 8, Java introduced a new method named join() in String object, help us can convert from List object to String object easily. Java
Example: Finally, we can use an existing libraryApache Commons Langof Apache Foundation. This libraryprovides a static method, namedjoin()in StringUtils classto help us can convert a List object to String object simply and easily. Because of its static method, then we only need call: Java
Example: |