• 源码分析之Iterable&Collection(一)


    Iterable

      Iterable是迭代器的意思,作用是为集合类提供for-each循环的支持。由于使用for循环需要通过位置获取元素,而这种获取方式仅有数据支持,其他数据结构,比如链表,只能通过查询获取数据,会降低效率。Iterable就可以让不同的集合类自己提供遍历的最佳方式

    /**
     * Implementing this interface allows an object to be the target of
     * the "for-each loop" statement. See
     */
    public interface Iterable<T> {
        /**
         * Returns an iterator over elements of type {@code T}.
         * @return an Iterator.
         */
        Iterator<T> iterator();
    
        /**
         * Performs the given action for each element of the {@code Iterable}
         * until all elements have been processed or the action throws an
         * exception.  Unless otherwise specified by the implementing class,
         * actions are performed in the order of iteration (if an iteration order
         * is specified).  Exceptions thrown by the action are relayed to the
         * caller.
         *
         * @since 1.8
         */
        default void forEach(Consumer<? super T> action) {
            Objects.requireNonNull(action);
            for (T t : this) {
                action.accept(t);
            }
        }
    
        /**
         * Creates a {@link Spliterator} over the elements described by this
         * {@code Iterable}.
         * @since 1.8
         */
        default Spliterator<T> spliterator() {
            return Spliterators.spliteratorUnknownSize(iterator(), 0);
        }
    }
    

      

    Collection  

      CollectionListQueueSet的超集,它直接继承于Iterable,也就是所有的Collection集合类都支持for-each循环。除此之外,Collection也是面向接口编程的典范,通过它可以在多种实现类间转换

    /**
     * The root interface in the <i>collection hierarchy</i>.  A collection
     * represents a group of objects, known as its <i>elements</i>.  Some
     * collections allow duplicate elements and others do not.  Some are ordered
     * and others unordered.  The JDK does not provide any <i>direct</i>
     * implementations of this interface: it provides implementations of more
     * specific subinterfaces like <tt>Set</tt> and <tt>List</tt>.  This interface
     * is typically used to pass collections around and manipulate them where
     * maximum generality is desired.
     **/
    public interface Collection<E> extends Iterable<E> {
        
    }
    

      

    AbstractCollection

      在Collection中定义的许多方法,根据现有的定义以及继承的Iterable,都可以在抽象类中实现,这样可以减少实现类需要实现的方法

    /**
     * This class provides a skeletal implementation of the <tt>Collection</tt>
     * interface, to minimize the effort required to implement this interface. <p>
     **/
    public abstract class AbstractCollection<E> implements Collection<E> {
    
    }
    

      

    Collections

      是一个包装类(工具类/帮助类)。它包含有各种有关集合操作的静态多态方法。此类不能实例化,就像一个工具类,用于对集合中元素进行排序、搜索以及线程安全等各种操作,服务于Java的Collection框架。

    public class Collections {
        // Suppresses default constructor, ensuring non-instantiability.
        private Collections() {
        }
    }
  • 相关阅读:
    Leetcode#129 Sum Root to Leaf Numbers
    Leetcode#15 3Sum
    Leetcode#16 3Sum Closest
    Leetcode#127 Word Ladder
    Leetcode#2 Add Two Numbers
    Leetcode#18 4Sum
    vue.js入门(3)——组件通信
    vue.js慢速入门(2)
    vue 基础介绍
    vue.js中v-for的使用及索引获取
  • 原文地址:https://www.cnblogs.com/ryjJava/p/14031510.html
Copyright © 2020-2023  润新知