convert List To String - Java java.lang

Java examples for java.lang:String Join

Description

convert List To String

Demo Code

//package com.java2s;

import java.util.Iterator;
import java.util.List;

public class Main {
    public static String convertListToString(List<String> myList) {
        return convertListToStringWithSeparator(myList, " ");
    }//from   w ww  . j  ava  2s.  com

    public static String convertListToStringWithSeparator(
            List<String> myList, String separator) {
        StringBuilder result = new StringBuilder();

        Iterator<String> iter = myList.iterator();
        while (iter.hasNext()) {
            String item = iter.next();

            // append each list item to the result string
            result.append(item);

            // do this only if we have more
            if (iter.hasNext()) {
                result.append(separator);
            }
        }
        return result.toString();
    }
}

Related Tutorials