首页 > 解决方案 > Java中的List.of()生成什么类型​​的列表

问题描述

Employee john = new Employee("John", "Brown", 32, 100);
Employee camila = new Employee("Camila", "Smith", 25, 101);
Employee pat = new Employee("Pat", "Hanson", 23, 102);

List<Employee> employeeList = List.of(john, camila, pat);

List方法生成什么类型List.of()​​。是一个ArrayList还是一个LinkedList

标签: javalistcollections

解决方案


两者都不。根据List.of()它返回的文档:

返回包含任意数量元素的不可变列表。

请注意,它是实现的接口 ListArrayListLinkedList

如果我们运行这段代码:

List<Integer> listOf = List.of(1,2,3);
System.out.println(listOf.getClass());

我们得到:

class java.util.ImmutableCollections$ListN

推荐阅读