(i) Collection and collections
Both are located under the Java.util package, and the difference is:
Collection is a set interface, with Listset and other common Sub-interfaces, is the first node of the collection frame diagram, and provides a series of methods for basic operation of the collection object.
The common methods are:
Boolean Add (E) adds an element to the container; int size () returns the number of elements of the collection; Boolean IsEmpty () determines whether the container is empty; Boolean contains (Object o) if this collecti On contains the specified element, returns True, where the Equals () method is used, and the Boolean remove (Object O) Removes the instance of the specified element;
The collections is a wrapper class that contains a variety of static polymorphic methods for set operations, containing a polymorphic algorithm that operates on collection, the "wrapper", which returns a new collection supported by the specified collection, and a few other content.
The common methods are:
The void sort (list) sorts the contents of the list.
Note here that (PS: The following description of sort () is excerpted from the discussion of object array or list sorting and collections sorting principle , to the list and collection sorting, write very clearly)
The sort body in this sort () 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 (), can be seen through the comparabletimsort. The sort (object[] a) implements:
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 size comparison section in Binarysort is:
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 comparison size part of the binary lookup uses the only method of the comparable interface: CompareTo (), which implements the comparable interface or inherits the comparator class if the custom class is loaded into the container and needs to be compared. and rewrite the CompareTo () method.
int BinarySearch (List object) uses binary lookup to find the specified object for the Order list container, and void reverse (list) orders the objects in the list's container in reverse order;
(ii) iterator and iterable
First, the iterable is located under the Java.lang package, iterator under the Java.util package. In the set framework, three methods are defined in the iterator interface: Boolean hasnext (); E next (); void Remove (). In Iterable, only one method is defined: iterator (), which returns a value of an object that implements the iterator interface. Collection inherits the Iterable interface, so all implementation classes in the set frame have the iterator () method, and polymorphism lets iterator references access to the part of the current collection that implements the iterator (that is, the three methods). If you need to delete an element at this point, because the iterator completes the lock on this set operation, you can use only the iterator remove () method in the iterator loop, and you cannot use collection's own remove (Object) Method.
So why do we have to implement the Iterable interface, why not directly implement the iterator interface, so that the collection class can inherit the three methods directly?
Look at the collection classes in the JDK, such as list or set, which implement the Iterable interface, but do not implement the iterator interface directly.
It makes sense to think carefully.
Because the core method of the Iterator interface next () or Hasnext () is dependent on the current iteration position of the iterator.
If the collection directly implements the iterator interface, 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 preset.
Unless you add a reset () method to the iterator interface to reset the current iteration position.
But in the immediate case, collection can only have one current iteration position at the same time.
Iterable, however, returns an iterator that starts counting from scratch each time the call is taken.
Multiple iterators are not interfering with each other.
The above is based on the Java collection of some confusing knowledge points (detailed) is a small set to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.