美文网首页Java 杂谈JavaIT@程序员猿媛
Java填充List容器方法及源码介绍

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

作者: 仲冬初七 | 来源:发表于2019-04-16 10:19 被阅读3次

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);
    }

相关文章

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

    Collections提供了两个填充容器的静态方法fill,nCopies 用法 源码介绍 fill源码 nCop...

  • 搞懂 Java equals 和 hashCode 方法

    搞懂 Java equals 和 hashCode 方法 分析完 Java List 容器的源码后,本来想直接进入...

  • List、Set和Map

    java 常用集合list与Set、Map区别及适用场景总结Java中容器[Collection(List,Set...

  • 关于 Java List 容器的源码分析的补充

    关于 Java List 容器的源码分析的补充 之前我们通过分析源码的方式学习了 ArrayList 以及 Lin...

  • C++ STL 之 list(下)

    本节我们将继续介绍 STL 中的 list 容器使用。 list 容器排序及合并元素 sort() 函数定义在头文...

  • java容器之自定义ArrayList

    前言在Java中我们常常用到容器List、Set、map,在这里我重写Arraylist,以更好的了解java源码...

  • java基础总结

    文章 【1】java基础文章汇总 深入Java源码解析容器类List、Set、Map 【2】计算机网络中的TCP/...

  • Java集合总结

    一:List Java ArrayList源码学习 Java LinkedList源码学习 Java Vector...

  • List接口

    介绍&常用方法 java.util.List接口 extends Collection接口 List接口的特点: ...

  • Java面试题

    Java基础 容器 1.Java容器都有哪些 总体分为Collection 、Map,细分为List、Set、Ma...

网友评论

    本文标题:Java填充List容器方法及源码介绍

    本文链接:https://www.haomeiwen.com/subject/yduvwqtx.html