List<Integer>,List<String>之间互相转换

public class Test {
    public static void main(String[] args) {
        List<Integer> integerList = new ArrayList<>();
        integerList.add(1);
        integerList.add(2);
        integerList.add(3);
        // List<Integer> 转为 List<String>,注意不要用toString
        List<String> stringList = integerList.stream().map(String::valueOf).collect(Collectors.toList());
        System.out.println("List<Integer> 转为 List<String>=====>" + stringList);
 
        //List<String> 转为 String 以逗号分隔
        String str = String.join(",", stringList);
        System.out.println("List<String> 转为 String 以逗号分隔====>" + str);
 
        //List<String> 转为 List<Integer>
        List<Integer> integerList1 = stringList.stream().map(Integer::valueOf).collect(Collectors.toList());
        System.out.println("List<String> 转为 List<Integer>====>" + integerList1);
    }
}

 

posted @ 2023-05-26 00:32  了悟  阅读(933)  评论(0编辑  收藏  举报