zoukankan      html  css  js  c++  java
  • Collections.singletonList方法

      这个方法主要用于只有一个元素的优化,减少内存分配,无需分配额外的内存,可以从SingletonList内部类看得出来,由于只有一个element,因此可以做到内存分配最小化,相比之下ArrayList的DEFAULT_CAPACITY=10个。

      

    源码:

     /**
         * Returns an immutable list containing only the specified object.
         * The returned list is serializable.
         *
         * @param  <T> the class of the objects in the list
         * @param o the sole object to be stored in the returned list.
         * @return an immutable list containing only the specified object.
         * @since 1.3
         */
        public static <T> List<T> singletonList(T o) {
            return new SingletonList<>(o);
        }

    使用:

      

    
    List<Integer> catalogIds = Collections.singletonList(catalogId);
  • 相关阅读:
    Unity做AR
    Linux怎么安装vim编译器
    Linux命令之tar
    Linux命令之ln
    Linux命令之grep
    Linux命令之less
    Linux命令之cd
    Linux命令之ll
    Linux命令之cp
    Linux命令之rm
  • 原文地址:https://www.cnblogs.com/crazy-lc/p/13724101.html
Copyright © 2011-2022 走看看