Java填充List容器方法及源码介绍

Collections提供了两个填充容器的静态方法fill,nCopies

用法

 // 创建一个存放int型的数组,同时填充四个整型对象1
        List<Integer> intArr = new ArrayList<Integer>(Collections.nCopies(4, 1));
        System.out.println(intArr.toString());
    //将intArr容器填充为2
        Collections.fill(intArr, 2);
        System.out.println(intArr.toString());

# out
[1, 1, 1, 1]
[2, 2, 2, 2]

源码介绍

fill源码

 /**
     * Replaces all of the elements of the specified list with the specified
     * element. <p>
     *
     * This method runs in linear time.
     *
     * @param  <T> the class of the objects in the list
     * @param  list the list to be filled with the specified element.
     * @param  obj The element with which to fill the specified list.
     * @throws UnsupportedOperationException if the specified list or its
     *         list-iterator does not support the <tt>set</tt> operation.
     */
    /**
    * list - 使用指定元素填充的列表。
    * obj - 用来填充指定列表的对象。    
    */
    public static <T> void fill(List<? super T> list, T obj) {
        int size = list.size();
        // 这里判断容器大小是否超过了默认设置的阀值25或者该容器是实现RandomAccess接口的
        if (size < FILL_THRESHOLD || list instanceof RandomAccess) {
            for (int i=0; i<size; i++)
                //直接填充
                list.set(i, obj);
        } else {
            //如果不是则调用List中的方法listIterator,返回迭代器进行设置,使用迭代器的目的是为了提高执行效率
            ListIterator<? super T> itr = list.listIterator();
            for (int i=0; i<size; i++) {
                itr.next();
                itr.set(obj);
            }
        }
    }

nCopies源码

 /**
     * Returns an immutable list consisting of <tt>n</tt> copies of the
     * specified object.  The newly allocated data object is tiny (it contains
     * a single reference to the data object).  This method is useful in
     * combination with the <tt>List.addAll</tt> method to grow lists.
     * The returned list is serializable.
     *
     * @param  <T> the class of the object to copy and of the objects
     *         in the returned list.
     * @param  n the number of elements in the returned list.
     * @param  o the element to appear repeatedly in the returned list.
     * @return an immutable list consisting of <tt>n</tt> copies of the
     *         specified object.
     * @throws IllegalArgumentException if {@code n < 0}
     * @see    List#addAll(Collection)
     * @see    List#addAll(int, Collection)
     */
    /**
    * n - 返回列表中的元素数, 即需要填充的数量。
    * o - 填充的对象。
    * 返回值:由指定对象 n 个副本组成的不可变列表。
    */
    public static <T> List<T> nCopies(int n, T o) {
        if (n < 0)
            throw new IllegalArgumentException("List length = " + n);
        return new CopiesList<>(n, o);
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第01章 JAVA简介第02章 基础语法第02章 递归补充第03章 面向对象第04章 异常处理第05章 数组第06...
    顺毛阅读 528评论 0 1
  • java基础 集合承继包含图 Collection vs Collections 首先,"Collection" ...
    onlyHalfSoul阅读 1,360评论 0 5
  • 学习了1.罗素:历史还不是一门科学,仅仅靠伪造和删节才会被弄得像门科学似的。2.@yegle:你以为你错过了Goo...
    江狒狒阅读 207评论 0 1
  • 阿竹今天对阿华说,阿晴带他见了一个叔叔,还带他吃了馄钝,吃着吃着,阿晴哭了,说想起了以前,想起了回忆。 阿晴看着眼...
    秦楚_8058阅读 496评论 0 0
  • 姓名:马新路 公司:上海铺天地 【日精进打卡第20天】 【知~勤学】 《六项精进》大纲5遍 《大学》大纲5遍 【行...
    新路JINYU阅读 118评论 0 0