(I) Collection and collections
Both are located under the java.util package, the difference is:
collection is a collection interface, with common subinterfaces such as ListSet, and is the first node of the collection framework graph. It provides a series of methods for performing basic operations on collection objects.
Common methods are:
boolean add(E e) Add elements 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 collection contains the specified element, it returns true, and the equals() method will be used here; boolean remove(Object o) removes the instance of the specified element; etc.
collections is a wrapper class that contains various static polymorphic methods for collection operations, which contains polymorphic algorithms operated on collection, i.e. "wrapper", which returns a new collection supported by the specified collection, and a few other things.
Common methods are:
void sort(List) sorts the contents of the List.
It should be noted here that (ps: The following explanation about sort() is excerpted from a brief discussion on object array or list sorting and Collections sorting principles . It traces the origin of List and Collection sorting, and writes it very clearly)
The sorting 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++]); } } In Arrays.sort(), it can be seen that it is implemented through ComparableTimSort.sort(Object[] a): 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 lo, int hi, int start). In binarySort, the part used for size comparison 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 only method of the Comparable interface is used in the binary search: compareTo(). If all custom classes are loaded into the container and need to be compared, you must implement the Comparable interface or inherit the Comparator class and override the compareTo() method.
int binarySearch(List object) For sequential List containers, the half-finding method is used to find the specified object; void reverse(List) arranges the objects in the List container in reverse order; etc.
(II) Iterator and Iterable
First, the Iterable is located under the java.lang package, and the 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(). Iterable only defines one method: iterator(), the return value is an object that implements the Iterator interface. Collection inherits the super interface of Iterable, so all implementation classes in the collection framework have the iterator() method, and polymorphism allows the reference of Iterator to access the part (that is, those three methods) in the current collection that implements Iterator. If you need to delete elements at this time, since Iterator completes locking on this collection operation, you can only use the Iterator's remove() method during the loop traversal with Iterator, and cannot use the Collection's own remove(Object) method.
So why do we have to implement the Iterable interface? Why not directly implement the Iterator interface? This way, the collection class can directly inherit these three methods?
Take a look at the collection classes in JDK, such as the List family or the Set family, which implement the Iterable interface, but do not directly implement the Iterator interface.
Think about it carefully and make sense.
Because the core methods of the Iterator interface next() or hasNext() depend on the current iteration position of the iterator.
If the Collection directly implements the Iterator interface, it will inevitably lead to the collection object containing the data (pointer) of the current iteration position.
When a collection is passed between different methods, since the current iteration position is not preset, the result of the next() method becomes unpredictable.
Unless a reset() method is added to the Iterator interface to reset the current iteration position.
But in this case, the Collection can only have one current iteration location at the same time.
Iterable is not the case, and each call returns an iterator counting from scratch.
Multiple iterators do not interfere with each other.
The above article is based on some confusing knowledge points (detailed explanation) in the Java collection. I hope it can give you a reference and I hope you can support Wulin.com more.