集合(Collection)

Java 的集合系列有两大块,一是Collection,二是Map:
先看看Collection接口和它的衍生接口和衍生类:

|Collection
   ||List
   ||Set
   ||Queue

First, let's check the JDK code of Collection, here I simplify the codes and comments, I'll note some code that I don't understand:

/*We can know that Collection is interface and extends from Iterable*/
public interface Collection extends Iterable{
////// Query Operations
   //Return the number of elements in the collection
   //If overflow, return Integer.MAX_VALUE
   int size();   

   //Return true if there is no element    
   boolean isEmpty();

   //Return true if contains the element
   boolean contains(Object o);
 
   //Returns an iterator over the elements in this collection
   Iterator iterator();

   //Returns an array containing all of the elements in this collection
   Object[] toArray();

   //???
    T[] toArray(T[] a);

 ////// Modification Operations
   //Return true if added, return false if collection does not permit 
   //duplicates and already contains the specified element(like Set)
   boolean add(E e);

   //Return true if removed.
   boolean remove(Object o);

////// Bulk Operations
   //Return true if contains all the elements in c
   boolean containsAll(Collection c);

   //Return true if all the elements in c added to this collection
   boolean addAll(Collection c);

   //Return true if all the elements in c removed from this collection
   boolean removeAll(Collection c);

   //Return true if all the elements satisfy the condition of filter removed
   default boolean removeIf(Predicate filter) {
}
   
   //Return true if all the elements that are not contained in c removed 
   boolean retainAll(Collection c);

   //Remove all the elements in this collection
   void clear();

//////// Comparison and hashing
   //Return true if two lists contains same elements with same order
   boolean equals(Object o);

   //Return the hashCode of this Collection
   int hashCode();

   //Create a spliterator over the elements in the Collection
   default Spliterator spliterator(){
}
  
   //???
    default Stream stream() {
        return StreamSupport.stream(spliterator(), false);
    }

  //???
    default Stream parallelStream() {
        return StreamSupport.stream(spliterator(), true);
    }
}

这些是JDK源码中Collection接口的一些方法,我简化了一下注释,具体的实现在ArrayList、LinkedList等类里面。
思考:
1.hashCode()的实现原理
2.iterator的实现原理

你可能感兴趣的:(集合(Collection))