A Collection and collections both are located under the Java.util package, the difference is: Collection is a collection interface, has listset and other common sub-interface, is the first node of the collection frame diagram, provides a series of methods for basic operation of the collection object. The common methods are: Boolean Add (E) To add elements to the container, int size () returns the number of collection elements, and Boolean isEmpty () to determine if this container is empty; Boolean contains (Object O) if this C Ollection contains the specified element, returns True, where the Equals () method is used, and a Boolean remove (Object O) Removes the instance of the specified element; Collections is a wrapper class that contains a variety of static polymorphic methods for set operations, which contain polymorphic algorithms that operate on collection, the "wrapper," which returns new collection supported by the specified collection, and a few other content. A common method is to sort the contents of a list by void sort (list).
It is important to note that (PS: The following description of sort () is excerpted from http://trinea.iteye.com/blog/1248517, and the list and collection are sorted backwards, and are clearly written) this sort () The sort body in the function is Arrays.sort (),
@SuppressWarnings ("unchecked") public static <t extends comparable<? super t>> void Sort (list<t> List) { object[] array = List.toarray (); Arrays.sort (array); int i = 0; Listiterator<t> it = List.listiterator (); while (It.hasnext ()) { it.next (); It.set ((T) array[i++]); }
and Arrays.sort (), it can be seen throughComparabletimsort.Sort (object[] a) implemented by:
public static void sort (object[] array) { //BEGIN android-changed comparabletimsort.sort (array); END android-changed }
static void sort (object[] a) to static void sort (object[] A, int lo, int hi) to private static void Binarysort (object[] A, int l o, int hi, int start). The parts used in Binarysort for the size comparison are:
comparable<object> pivot = (comparable) A[start]; int left = lo; int right = start; Assert left <= right; while (left < right) { int mid = (left + right) >>> 1; if (Pivot.compareto (A[mid) < 0) Right = mid; else left = mid + 1;
The only way to compare the size of a binary lookup is to use the comparable interface: CompareTo (), to implement the comparable interface or inherit the comparator class if a custom class is to be loaded into a container that needs to be compared. and override the CompareTo () method.
int BinarySearch (List object) for the ordered list container, finds the specified object using the binary lookup method, and the Void reverse (list) sorts the objects in the list's container in reverse order;
(ii) Iterator and iterable first, Iterable is located under the Java.lang package, iterator is located under the Java.util package. In the collection framework, three methods are defined in the iterator interface: Boolean hasnext (); E next (); void Remove (). In Iterable, only one method is defined: iterator (), the return value is an object that implements the iterator interface. Collection inherits the super interface of iterable, so all the implementation classes in the collection framework have iterator (), and polymorphism allows the iterator reference to access the part of the current collection that implements iterator (that is, the three methods). If you need to delete an element at this point, because iterator completes the lock on this set operation, you can only use the Remove () method of iterator in the process of looping through iterator, instead of using collection's own remove (Object) Method.
Then why do you have to implement the Iterable interface, why not directly implement the iterator interface, so you can let the collection class directly inherit these three methods?
Look at the collection classes in the JDK, such as the list family or set family, are implemented Iterable interface, but do not directly implement the iterator interface.
It makes sense to think carefully about it.
Because the core method of the Iterator interface next () or Hasnext () is dependent on the current iteration position of the iterator.
If collection implements the iterator interface directly, it is bound to cause the collection object to contain the data (pointers) of the current iteration position.
When a collection is passed between different methods, the result of the next () method becomes unpredictable because the current iteration position is not pre-provisioned.
Unless you add a reset () method to the iterator interface, you can reset the current iteration position.
But in this case, collection can only have one current iteration position at a time.
Instead, Iterable returns an iterator that counts from the beginning of each call. Multiple iterators are non-interfering.
Some confusing points of knowledge in the Java collection