我们创建一个Set为:
Set myset = new HashSet()
我们如何在Java中创建一个列表?
我们创建一个Set为:
Set myset = new HashSet()
我们如何在Java中创建一个列表?
当前回答
在Java 8中
创建一个固定大小的非空列表(不支持添加、删除等操作):
List<Integer> list = Arrays.asList(1, 2); // but, list.set(...) is supported
创建一个非空的可变列表:
List<Integer> list = new ArrayList<>(Arrays.asList(3, 4));
在Java 9中
使用一个新的List.of(…)静态工厂方法:
List<Integer> immutableList = List.of(1, 2);
List<Integer> mutableList = new ArrayList<>(List.of(3, 4));
在Java 10中
使用局部变量类型推断:
var list1 = List.of(1, 2);
var list2 = new ArrayList<>(List.of(3, 4));
var list3 = new ArrayList<String>();
并遵循最佳实践……
不要使用原始类型
从Java 5开始,泛型就成为了语言的一部分——你应该使用它们:
List<String> list = new ArrayList<>(); // Good, List of String
List list = new ArrayList(); // Bad, don't do that!
编程到接口
例如,编程到List接口:
List<Double> list = new ArrayList<>();
而不是:
ArrayList<Double> list = new ArrayList<>(); // This is a bad idea!
其他回答
列表可以通过多种方式创建:
1 -构造函数初始化
List是一个接口,可以通过以下方式创建List实例:
List<Integer> list=new ArrayList<Integer>();
List<Integer> llist=new LinkedList<Integer>();
List<Integer> stack=new Stack<Integer>();
2-使用Arrays.asList()
List<Integer> list=Arrays.asList(1, 2, 3);
3-使用Collections类方法
空列表
List<Integer> list = Collections.EMPTY_LIST;
OR
List<Integer> list = Collections.emptyList();
Collections.addAll(list = new ArrayList<Integer>(), 1, 2, 3, 4);
无法改变的列表
List<Integer> list = Collections
.unmodifiableList(Arrays.asList(1, 2, 3));
单例对象列表
List<Integer> list = Collections.singletonList(2);
你可以从下面的参考链接中找到更多的方法。
参考:
https://www.geeksforgeeks.org/initializing-a-list-in-java/
//simple example creating a list form a string array
String[] myStrings = new String[] {"Elem1","Elem2","Elem3","Elem4","Elem5"};
List mylist = Arrays.asList(myStrings );
//getting an iterator object to browse list items
Iterator itr= mylist.iterator();
System.out.println("Displaying List Elements,");
while(itr.hasNext())
System.out.println(itr.next());
因为Java 7创建泛型实例有类型推断,所以不需要在赋值的右边复制泛型参数:
List<String> list = new ArrayList<>();
固定大小的列表可以定义为:
List<String> list = Arrays.asList("foo", "bar");
对于不可变列表,你可以使用Guava库:
List<String> list = ImmutableList.of("foo", "bar");
就像java中数组列表的声明一样
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
有许多方法可以在java中创建和初始化数组列表。
1) List list = new ArrayList();
2) List<type> myList = new ArrayList<>();
3) List<type> myList = new ArrayList<type>();
4) Using Utility class
List<Integer> list = Arrays.asList(8, 4);
Collections.unmodifiableList(Arrays.asList("a", "b", "c"));
5) Using static factory method
List<Integer> immutableList = List.of(1, 2);
6) Creation and initializing at a time
List<String> fixedSizeList = Arrays.asList(new String[] {"Male", "Female"});
Again you can create different types of list. All has their own characteristics
List a = new ArrayList();
List b = new LinkedList();
List c = new Vector();
List d = new Stack();
List e = new CopyOnWriteArrayList();
List和Set一样只是一个接口。
HashSet是Set的一种实现,它具有添加/查找/删除性能方面的某些属性,ArrayList是List的裸实现。
如果你看了相关接口的文档,你会发现“所有已知的实现类”,你可以决定哪一个更适合你的需求。
可能是数组列表。