个人技术分享

之前一直使用:

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

后来学到了一个新的方法:

List<String> list = Collections.emptyList();

如果不准备对list进行修改,只用来返回值。那么第二个方法更好。因为第二个方法生成的空list有三个优点:

  1. 不可修改(immutable);
  2. 效率高(more efficient)。因为不需要分配内存(does not require memory allocation);
  3. 单例模式(Singleton Pattern),每次返回的是同一个list(always returns the same instance)。