假设以下是我们的ArrayList-List arrList = Arrays.asList("John", "Jacob", "Kevin", "Katie", "Nathan");
现在,使用toCollection()将此ArrayList转换为LinkedList-List 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 arrList = Arrays.asList("John", "Jacob", "Kevin", "Katie", "Nathan");
System.out.println("ArrayList = " + arrList);
List 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]