解释Collections的语法。< String> emptyList() [英] Explain the syntax of Collections.<String>emptyList()

查看:115
本文介绍了解释Collections的语法。< String> emptyList()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚研究了泛型编程, List< E> 接口和 ArrayList ,所以我可以理解以下语句。

  ArrayList< String> list = new ArrayList< String>(); 

但我不明白我在浏览网页时看到的下一个声明。

 列表< String> list2 = Collections。< String> emptyList(); 




  1. 什么是集合?为什么它不是 Collections< E> Collections< String>

  2. 为什么< String> 放置在方法名称 emptyList 之前?

(不是 emptyList< String>()是否正确?)


  1. 这个陈述意味着什么?


解决方法

该行通过调用具有泛型类型参数的静态方法创建一个空字符串列表。



集合类,有一个静态方法 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();

  1. What is Collections? Why isn't it Collections<E> or Collections<String>?
  2. Why is <String> placed before the method name emptyList?

(Isn't emptyList<String>() correct for Generic?)

  1. 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的语法。&lt; String&gt; emptyList()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆