A great God once said: the best way to learn programming is to read the source code. Collection classes are undoubtedly the raw materials for learning the data structure. I started to prepare STL for using c ++. Due to my limited ability, I checked the STL source code of vc, what is a heavy pointer, a double pointer, and n heavy pointers all over the sky, so I had to give up. Java has no pointer, and the learning difficulty is undoubtedly greatly reduced. STL will only be able to improve later. The source code of the Collection class can be found in the src.zip file in the jdkinstallation directory. Due to limited space, a large number of unnecessary comments are deleted. First, let's look at Collection. java file [java]/** Copyright (c) 1997,201 0, Oracle and/or its affiliates. all rights reserved. * oracle proprietary/CONFIDENTIAL. use is subject to license terms. */package java. util;/** 1. the Collection interface is the root interface in the Set inheritance relationship. Some sets allow repeated elements. * Some sets are ordered, and JDK does not provide the implementation of this interface, only implement sub-interfaces (such as Set and List) * 2. all classes that implement Collection (or its sub-interfaces) must contain two constructor: A constructor without parameters, and * A copy constructor with parameters of Collection **/public interface Collection <E> extends Iterable <E >{// return elements in the Collection. If the number of elements exceeds Integer. MAX_VALUE, returns Integer. MAX_VALUE int size (); // returns true boolean isEmpty () if the set does not contain any elements. // If the set contains at least one specified object, returns true boolean contains (Object o); // returns the Iterator <E> iterator (); // returns an array composed of all elements in the set, the returned sequence of array elements must be the same as that of the elements returned by the iterator when accessing the collection. Object [] toArray (); // returns an array containing all elements in the collection; the runtime type of the returned array is the same as the runtime type of the specified array. <T> T [] toArray (T [] a); // If the set does not allow repeated elements and the Set already contains this element, false boolean add (E) is returned ); // remove a single instance of a specified element from this collection. If a specified element exists in the collection, true is returned. Boolean remove (Object o); // returns true if the collection contains all elements in the specified collection. Boolean containsAll (Collection <?> C); // Add all elements in the specified collection to this collection boolean addAll (Collection <? Extends E> c); // remove all elements in the collection that are also included in the specified collection (optional ). Boolean removeAll (Collection <?> C); // retain only the elements in the collection that are also included in the specified collection (optional ). Boolean retainAll (Collection <?> C); // remove all elements from the collection (optional ). Void clear (); // compare whether the collection is equal to the specified object. By overwriting, the list and list are equal, and the set and set are equal. boolean equals (Object o); // return the hash code value of this collection. Int hashCode ();} below is Iterator. java file [java] package java. util;/** there are two differences between Iterator and enumerations: * 1. the Iterator operation caller deletes the set element during iteration * 2. iterator changes the function name */public interface Iterator <E> {// returns true if there are still elements that can be iterated. Boolean hasNext (); // returns the next element of the iteration, E next (); // removes the last element returned by the iterator from the collection indicated by the iterator (optional ). Void remove () can be called only once in each next process.} the preceding two interfaces must be basically implemented in the java Collection class. Collection provides some necessary methods of the Collection class for polymorphism. The Iterator class provides the traversal of Collection class elements for encapsulation.