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);
  • 相关阅读:
    第十三周
    意见评论
    第十二周
    冲刺10
    冲刺9
    冲刺8
    团队冲刺第二十二天-KeepRunningAPP
    找水王
    第十四周总结
    搜狗输入法评价
  • 原文地址:https://www.cnblogs.com/crazy-lc/p/13724101.html
Copyright © 2011-2022 走看看