Java 中将 ArrayList 转换为 LinkedList 的程序
java 8object oriented programmingprogramming更新于 2025/12/9 3:24:17
假设我们的 ArrayList 减法如下
List<String> arrList = Arrays.asList("John", "Jacob", "Kevin>, "Katie", "Nathan");
现在,使用 toCollection() 将此 ArrayList 转换为 LinkedList 减法
List<String> myList = arrList.stream().collect(Collectors.toCollection(LinkedList::new));
示例
以下是 Java 中将 ArrayList 转换为 LinkedList 的程序 −
import java.util.*;
import java.util.stream.*;
public class Demo {
public static void main(String args[]) {
List<String> arrList = Arrays.asList("John", "Jacob", "Kevin", "Katie", "Nathan");
System.out.println("ArrayList = " + arrList);
List<String> myList = arrList.stream().collect(Collectors.toCollection(LinkedList::new));
System.out.println("LinkedList (ArrayList to LinkedList) = " + myList);
}
}
输出
ArrayList = [John, Jacob, Kevin, Katie, Nathan] LinkedList (ArrayList to LinkedList) = [John, Jacob, Kevin, Katie, Nathan]

