List转换String,String转List的几种方法

一、List转String的方法

将一个Java集合List转换为String方法比较多,可以使用String.join()、StringBuilder、Stream流等方法。下面举几个常用的示例:

1.使用String.join()方法
import java.util.ArrayList;
import java.util.List;

public class ListToStringExample {
    public static void main(String[] args) {
        List<String> myList = new ArrayList<>();
        myList.add("apple");
        myList.add("banana");
        myList.add("orange");

        String result = String.join(",", myList);
        System.out.println(result);
    }
}
2.使用循环和字符串拼接的方式将列表转换为字符串
javaCopy Codeimport java.util.ArrayList;
import java.util.List;

public class ListToStringExample {
    public static void main(String[] args) {
        List<String> myList = new ArrayList<>();
        myList.add("apple");
        myList.add("banana");
        myList.add("orange");

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < myList.size(); i++) {
            stringBuilder.append(myList.get(i));
            if (i != myList.size() - 1) {
                stringBuilder.append(",");
            }
        }

        String result = stringBuilder.toString();
        System.out.println(result);
    }
}
3.使用Java 8的Stream API来将列表转换为字符串
javaCopy Codeimport java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ListToStringExample {
    public static void main(String[] args) {
        List<String> myList = new ArrayList<>();
        myList.add("apple");
        myList.add("banana");
        myList.add("orange");

        String result = myList.stream().collect(Collectors.joining(","));
        System.out.println(result);
    }
}
4.使用Apache Commons Lang库的StringUtils
javaCopy Codeimport org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;

public class ListToStringExample {
    public static void main(String[] args) {
        List<String> myList = new ArrayList<>();
        myList.add("apple");
        myList.add("banana");
        myList.add("orange");

        String result = StringUtils.join(myList, ",");
        System.out.println(result);
    }
}

二、String转List的方法

1.使用String.split()方法
javaCopy Codeimport java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StringToListExample {
    public static void main(String[] args) {
        String str = "apple,banana,orange";
        List<String> myList = new ArrayList<>(Arrays.asList(str.split(",")));

        System.out.println(myList);
    }
}

需要注意的是,Arrays.asList()方法返回的是一个固定大小的列表,不能进行添加或删除元素的操作。如果您需要对列表进行修改操作,可以使用new ArrayList<>(Arrays.asList(...))来创建一个新的可修改的列表。

2.使用Java 8的Stream
javaCopy Codeimport java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StringToListExample {
    public static void main(String[] args) {
        String str = "apple,banana,orange";
        List<String> myList = Arrays.stream(str.split(","))
                                   .collect(Collectors.toList());

        System.out.println(myList);
    }
}
3.使用Apache Commons Lang库的StringUtils
javaCopy Codeimport org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;

public class StringToListExample {
    public static void main(String[] args) {
        String str = "apple,banana,orange";
        List<String> myList = new ArrayList<>(StringUtils.split(str, ","));

        System.out.println(myList);
    }
}
4.使用Java 8的正则表达式
javaCopy Codeimport java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringToListExample {
    public static void main(String[] args) {
        String str = "apple,banana,orange";
        List<String> myList = new ArrayList<>();
        Pattern pattern = Pattern.compile(",");
        Matcher matcher = pattern.matcher(str);

        int startPos = 0;
        while (matcher.find()) {
            String item = str.substring(startPos, matcher.start());
            myList.add(item);
            startPos = matcher.end();
        }
        String lastItem = str.substring(startPos);
        myList.add(lastItem);

        System.out.println(myList);
    }
}