zoukankan      html  css  js  c++  java
  • java源码 --List、Set、Collection

      List和Set都是接口,它们继承与Collection。List是有序的队列,可以用重复的元素;而Set是数学概念中的集合,不能有重复的元素。List和Set都有它们各自的实现类。

      为了方便,我们抽象出AbstractCollection类来让其他类继承,该类实现类Collection中的绝大部分方法。AbstractListAbstractSet继承AbstractCollection,具体的List实现类继承与AbstractList,而Set的实现类则继承与AbstractSet

       另外,Collection中有个iterator()方法,它的作用是返回一个Iterator接口。通常,我们通过Iterator迭代器来遍历集合。ListIteratorList接口所特有的,在List接口中,通过ListIterator()返回一个ListIterator对象

    Collection接口

      public interface Collection<E> extends Iterable<E> {} 它是一个高度抽象出来的集合,包含了集合的基本操作:添加、删除、清空、遍历、是否为空、获取大小等。

    普通方法:

      int size():返回此集合中的元素数量
      boolean isEmpty():判断集合元素是否为空
      boolean contains(Object o):判断此集合是否包含指定的元素,包含则返回true,反之
      Iterator<E> iterator():返回此集合中元素的迭代器,不保证元素返回的顺序(除非此集合是提供保证的某个类的实例)。
      Object[] toArray():将此集合中的元素转换成数组并返回该数组,该方法作为基于数组和集合之间的桥梁的api
      <T> T[] toArray(T[] a):返回指定类型数组
      boolean add(E e):此集合添加指定元素
      boolean remove(Object o):删除指定元素
      boolean containsAll(Collection<?> c):判断是否包含特定集合,如果存在则返回true 反之
      boolean addAll(Collection<? extends E> c):添加指定集合
      boolean removeAll(Collection<?> c):删除指定集合
      boolean retainAll(Collection<?> c):仅保留指定类型的集合
      void clear():清空集合元素
      boolean equals(Object o):将指定的对象与此集合进行相等性较
      int hashCode():返回集合的哈希值,用于比较相等与否

    默认方法:
      default boolean removeIf(Predicate<? super E> filter):移除此集合中满足给定条件的所有元素
      default Spliterator<E> spliterator( return Spliterators.spliterator(this, 0)):
      default Stream<E> stream() { return StreamSupport.stream(spliterator(), false);}返回一个输入流
      default Stream<E> parallelStream() { return StreamSupport.stream(spliterator(), true);}返回一个并行输入流

    List接口

      public interface List<E> extends Collection<E> {}

       从List定义中可以看出,它继承与Collection接口,即List是集合的一种。List是有序的队列,List中的每一个元素都有一个索引,第一个元素的索引值为0,往后的元素的索引值依次+1.,List中允许有重复的元素。

       List继承Collection自然包含了Collection的所有接口,由于List是有序队列,所以它也有自己额外的API接口。API如下:

      普通方法:

        add(int index,E element):在列表指定位置插入指定元素
        addAll(int index,Collection<? extends E> c):在指定位置添加集合元素
        get(int index):获取指定位置的元素
        indexOf(Object o):获取元素的第一个位置
        isEmpty():判断列表中是否有元素
        lastIndexOf(Object o):获取指定元素的最后一个位置
        listIterator():获取列表元素的列表迭代器
        listIterator(int index):从指定位置开始获取列表迭代器
        remove(int index):移除指定位置的元素
        set(int index,E element):用指定元素替换指定位置的元素
        subList(int fromIdex,int toIndex):获取fromIndex到toIndex的元素列表
      默认方法

    //将集合中的元素全部替换为函数值
    default void replaceAll(UnaryOperator<E> operator) {
            Objects.requireNonNull(operator);
            final ListIterator<E> li = this.listIterator();
            while (li.hasNext()) {
                li.set(operator.apply(li.next()));
            }
        }
    //将集合中的内容重新排序
    default void sort(Comparator<? super E> c) {
            Object[] a = this.toArray();
            Arrays.sort(a, (Comparator) c);
            ListIterator<E> i = this.listIterator();
            for (Object e : a) {
                i.next();
                i.set((E) e);
            }
        }
    default Spliterator<E> spliterator() {
            return Spliterators.spliterator(this, Spliterator.ORDERED);
        }

    Set接口

      public interface Set<E> extends Collection<E> {}

      Set也继承与Collection接口,且里面不能有重复元素。关于API,Set与Collection的API基本一样,不在赘述。

  • 相关阅读:
    668. Kth Smallest Number in Multiplication Table
    658. Find K Closest Elements
    483. Smallest Good Base
    475. Heaters
    454. 4Sum II
    441. Arranging Coins
    436. Find Right Interval
    410. Split Array Largest Sum
    392. Is Subsequence
    378. Kth Smallest Element in a Sorted Matrix
  • 原文地址:https://www.cnblogs.com/FondWang/p/11921321.html
Copyright © 2011-2022 走看看