Collections.emptyList()和Collections.EMPTY_LIST之间的区别是什么 [英] What is the difference between Collections.emptyList() and Collections.EMPTY_LIST

查看:724
本文介绍了Collections.emptyList()和Collections.EMPTY_LIST之间的区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我们有 Collections.emptyList( ) Collections.EMPTY_LIST 。两者都具有相同的属性:

In Java, we have Collections.emptyList() and Collections.EMPTY_LIST. Both have the same property:


返回空列表(不可变)。这个列表是可序列化的。

Returns the empty list (immutable). This list is serializable.

那么使用一个或另一个有什么区别?

So what is the exact difference between using the one or the other?

推荐答案


  • Collections.EMPTY_LIST 返回旧式 List

  • Collections.emptyList()使用类型推论因此返回
    List< T>

    • Collections.EMPTY_LIST returns an old-style List
    • Collections.emptyList() uses type-inference and therefore returns List<T>
    • Collections.emptyList()是在Java 1.5中添加的,它可能总是优先

      Collections.emptyList()本质上

      @SuppressWarnings("unchecked")
      public static final <T> List<T> emptyList() {
          return (List<T>) EMPTY_LIST;
      }
      

      这篇关于Collections.emptyList()和Collections.EMPTY_LIST之间的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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