java Collection 转 list
Java中可以使用Collection接口的toArray()方法将Collection转换为数组,然后再使用Arrays.asList()方法将数组转换为List。
例如:
import java.util.*;
public class CollectionToListExample {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("apple");
collection.add("banana");
collection.add("orange");
// 将Collection转换为数组
String[] array = collection.toArray(new String[collection.size()]);
// 将数组转换为List
List<String> list = Arrays.asList(array);
System.out.println(list);
}
}
输出结果为:
[apple, banana, orange]
原文地址: https://www.cveoy.top/t/topic/bjj8 著作权归作者所有。请勿转载和采集!