Java 中将 ArrayList 转换为 LinkedList 的程序
假设以下是我们的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]
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短