java source --List, Set, Collection

  List and Set interfaces are, they inherit the Collection. List is orderly queue, you can use repetitive elements; and Set is a collection of mathematical concepts, can not have duplicate elements. Set List and have their own implementation class.

  For convenience, we AbstractCollection abstract class to allow other class inheritance, which implements most of the Method in class Collection. AbstractList and AbstractSet both inherited and AbstractCollection , concrete List implementation class inheritance and AbstractList , and Set implementation class inherit and AbstractSet .

   Further, there is the Collection iterator () method, its role is to return an Iterator interface. Usually, we pass Iterator iterator to traverse the collection. ListIterator is List interface unique to the interface in the List by ListIterator () Returns a ListIterator objects .

Collection Interface

  public interface Collection < E> the extends the Iterable < E> {} which is a set of highly abstracted, comprising a set of basic operations: add, delete, clear, traverse, is empty, and acquires size.

Common methods:

  int size (): Returns the number of elements in this set
  boolean isEmpty (): determining whether the empty collection element
  boolean contains (Object o): determines whether this collection contains the specified element, comprising Returns true, and vice versa
  Iterator <E> iterator (): returns an iterator this collection of elements, does not guarantee the order (unless this collection is to provide a guarantee of instances of a class) are returned.
  Object [] toArray (): convert this set of elements in the array and returns the array, based on the method as a bridge between the array and the set of api
  <T> T [] toArray (T [] a): Returns an array of the specified type
  boolean add (E e): Add this collection specified element
  boolean remove (Object o): delete the specified element
  boolean containsAll (Collection c <?>): comprising determining whether a particular set, returns true if present, and vice versa
  boolean addAll (Collection <extends E?> c): Adds the specified collection
  boolean removeAll (Collection c <?>): delete the specified collection
  boolean retainAll (Collection c <?>): keep only the collection of the specified type
  void clear (): Empty the collection element
  boolean equals (Object o): The specified object of this collection compared for equality
  int hashCode (): returns the hash value set for comparison for equality 

default method:
  default boolean removeIf (Predicate filter <super E?>): remove this collection meet the conditions set for all the elements
  default Spliterator<E> spliterator( return Spliterators.spliterator(this, 0)):
  default Stream <E> stream () {return StreamSupport.stream (spliterator (), false);} return an input stream
  default Stream <E> parallelStream () {return StreamSupport.stream (spliterator (), true);} return a parallel input stream

List Interface

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

   List can be seen from the definition, it inherits the Collection interface , i.e., a set of List. List are ordered queue, List Each element has an index, the index of the first element is 0, the index value of the next element in turn + 1, duplicate elements List allows.

   List of natural succession Collection contains all of the interface Collection , due List is orderly queue, so it also has its own additional API interface. API as follows:

  Common methods:

    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基本一样,不在赘述。

Guess you like

Origin www.cnblogs.com/FondWang/p/11921321.html