解释Collections的语法。< String> emptyList() [英] Explain the syntax of Collections.<String>emptyList()
问题描述
我刚刚研究了泛型编程, List< E>
接口和 ArrayList
,所以我可以理解以下语句。
ArrayList< String> list = new ArrayList< String>();
但我不明白我在浏览网页时看到的下一个声明。
列表< String> list2 = Collections。< String> emptyList();
- 什么是
集合
?为什么它不是Collections< E>
或Collections< String>
? - 为什么
< String>
放置在方法名称emptyList
之前?
(不是 emptyList< String>()
是否正确?)
- 这个陈述意味着什么?
该行通过调用具有泛型类型参数的静态方法创建一个空字符串列表。
在集合
类,有一个静态方法 emptyList
声明为:
public static final< T>列表与LT; T> emptyList(){
return(List< T>)EMPTY_LIST;
}
这有一个泛型类型参数 T
。我们使用以下方法调用此方法:
List< String> list = Collections.emptyList();
和 T
被感染为 String
因为 list
类型。
我们可以当调用 emptyList
时,还可以指定 T
的类型,方法是将它放在尖括号中。如果我们想要推断出更具体的类型,那么可能需要这样做:
List <?扩展对象> list = Collections。< String> emptyList();
emptyList< String>()
不是正确,因为只有在创建
泛型类的实例时,该放置才有效,而不是调用方法。当使用 new
时,有两个可能的
类型参数,类名前面的参数仅用于构造函数,而类名后面的参数是整个实例,所以类:
class MyClass< A> {
public< B> MyClass(A a,B b){
System.out.println(a +,+ b);
$ b $ p
$ b 我们可以调用它的构造函数,其中 A
是字符串
和 B
是整数
like:
MyClass< String> a = new< Integer> MyClass< String>(a,3);
或使用类型推断:
MyClass的<字符串> a = new MyClass<>(a,3);
另请参阅:
I just studied about generic programming, the List<E>
interface, and ArrayList
, so I can understand the statement below.
ArrayList<String> list = new ArrayList<String>();
But I don't understand the next statement which I saw while surfing the web.
List<String> list2 = Collections.<String>emptyList();
- What is
Collections
? Why isn't it Collections<E>
or Collections<String>
?
- Why is
<String>
placed before the method name emptyList
?
(Isn't emptyList<String>()
correct for Generic?)
- What does the statement mean?
解决方案 That line creates an empty list of strings by calling a static method with a generic type parameter.
Inside the Collections
class, there is a static method emptyList
declared like:
public static final <T> List<T> emptyList() {
return (List<T>) EMPTY_LIST;
}
This has a generic type parameter T
. We call call this method by using:
List<String> list = Collections.emptyList();
and T
is infered to be a String
because of the type of list
.
We can also specify the type of T
by putting it in angle brackets when calling emptyList
. This may be needed if we want a more specific type than is inferred:
List<? extends Object> list = Collections.<String>emptyList();
emptyList<String>()
is not correct because that placement is only valid when creating
instances of generic classes, not calling methods. When using new
there are two possible
type parameters, the ones before the class name are for the constructor only, and the ones after the class name are for the whole instance, so with the class:
class MyClass<A> {
public <B> MyClass(A a, B b) {
System.out.println(a + ", " + b);
}
}
We can call its constructor where A
is String
and B
is Integer
like:
MyClass<String> a = new <Integer>MyClass<String>("a", 3);
or by using type inference:
MyClass<String> a = new MyClass<>("a", 3);
See also:
这篇关于解释Collections的语法。< String> emptyList()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!