Collection

  • Post author:
  • Post category:其他


public interface Collection<E> extends Iterable<E> {
    // 查询操作
    
    // 当前集合元素数目
    int size();
		// 集合是否为空
    boolean isEmpty();
		// 集合是否包含某个元素
    boolean contains(Object o);
		// 获得迭代器    
    Iterator<E> iterator();
		// 当前集合转化为数组
    Object[] toArray();
		// 特定泛型数组 转为该泛型数组
    <T> T[] toArray(T[] a);

    // 写操作
    
		// 增加元素
    boolean add(E e);
		// 删除元素
    boolean remove(Object o);


    // Bulk Operations
		// 批量操作
   	
    // 当前集合是否包含 指定集合所有元素
    boolean containsAll(Collection<?> c);
		// 批量增加指定集合中的元素
    boolean addAll(Collection<? extends E> c);
		// 批量删除
    boolean removeAll(Collection<?> c);
		// 批量删除 满足条件的元素
    default boolean removeIf(Predicate<? super E> filter) {
        Objects.requireNonNull(filter);
        boolean removed = false;
        final Iterator<E> each = iterator();
        while (each.hasNext()) {
            if (filter.test(each.next())) {
                each.remove();
                removed = true;
            }
        }
        return removed;
    }

    /**
     * Retains only the elements in this collection that are contained in the
     * specified collection (optional operation).  In other words, removes from
     * this collection all of its elements that are not contained in the
     * specified collection.
     *
     * @param c collection containing elements to be retained in this collection
     * @return <tt>true</tt> if this collection changed as a result of the call
     * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
     *         is not supported by this collection
     * @throws ClassCastException if the types of one or more elements
     *         in this collection are incompatible with the specified
     *         collection
     *         (<a href="#optional-restrictions">optional</a>)
     * @throws NullPointerException if this collection contains one or more
     *         null elements and the specified collection does not permit null
     *         elements
     *         (<a href="#optional-restrictions">optional</a>),
     *         or if the specified collection is null
     * @see #remove(Object)
     * @see #contains(Object)
     */
    boolean retainAll(Collection<?> c);

    // 清除所有元素
    void clear();


    // Comparison and hashing
    // 比较以及哈希方法

		// 比较当前集合 与目标集合是否equals
    boolean equals(Object o);
		// 哈希方法
    int hashCode();

    // 集合拆分
    @Override
    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);
    }
}



版权声明:本文为csdn_9527666原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。