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/mLpf 著作权归作者所有。请勿转载和采集!