1. iterator iterators
All collection implementation classes implement the Iterator method, which returns an object of the iterator interface type to facilitate the iteration of the collection element. Under the Java.util package.
1) Iterator definition has three methods:
①boolean Hasnext () method: Determines whether there are elements behind the pointer.
②e Next () method: The pointer moves back and returns the current element. E represents generics, and the default is type object.
③void Remove () method: Removes the element that was just returned in the original collection.
2) for the list collection, you can traverse through the Get method based on the subscript, whereas the iterator method is designed for the collection interface, so all classes that implement the collection interface can use iterator to implement iterative traversal.
3) How to use iterators: first ask and then take. Q: Boolean Hasnext () This method asks if the current collection of iterators has any elements; Take: E Next () The method gets the current element. The iterative approach to iterators is tailored to the while loop.
4) Removal problem in iterators: In the iterative process, we cannot change the number of elements in the collection by adding or removing the "collection". Otherwise, an iterative exception will be thrown! If you want to delete an iterative element, you can only pass iterator. When an iterator uses its own remove () method, it can remove the element just fetched from the collection, but it cannot be called repeatedly two times! That is, in the case of the incessantly generation, you cannot delete two times in one place.
Case 9:
650) this.width=650; "style=" width:668px;height:478px; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M01/6e/f8/wkiom1wnfvkxg5yaaakajavzksg077.jpg "width=" 654 "height=" 478 "alt=" wkiom1wnfvkxg5yaaakajavzksg077.jpg "/ >
2. comparable and comparator interface
(1) Comparable interface: The comparable interface is used to represent the size relationship between objects, we need to implement the comparable interface, and override the CompareTo () method to define the comparison rules.
(2) Collections.sort () method: Requires the object in the collection to implement the comparable interface, so that it can call its CompareTo method to determine the size of the object, otherwise sort will not be judged. The method invokes the CompareTo method of each element in the collection, in turn, and makes a natural sort.
Case 10:
650) this.width=650; "style=" width:678px;height:372px; "title=" 1 clipboard.png "src=" http://s3.51cto.com/wyfs02/ M02/6e/f8/wkiom1wngamyehioaahq91rkzy0951.jpg "width=" 715 "height=" 336 "alt=" wkiom1wngamyehioaahq91rkzy0951.jpg "/ >
650) this.width=650; "title=" 2 Clipboard.png "src=" http://s3.51cto.com/wyfs02/M00/6E/F4/wKioL1WNG36ztdQWAAGE_ Hycnb0496.jpg "width=" 678 "height=" 297 "alt=" wkiol1wng36ztdqwaage_hycnb0496.jpg "/>
650) this.width=650; "style=" width:680px;height:65px; "title=" 3 clipboard.png "src=" http://s3.51cto.com/wyfs02/ M01/6e/f8/wkiom1wngftsb2esaabzfc9wije684.jpg "width=" 664 "height=" "alt=" wkiom1wngftsb2esaabzfc9wije684.jpg "/ >
(3) Comparator interface: Comparator. Once the Java class implements the comparable, its comparison logic has been determined, if you want the operation in the sort to follow the "ad hoc rules", that is, the custom comparison rule. The comparator interface callback method can be used. It is common to use anonymous classes to create an instance to define the comparer.
Comparator Comparator creation steps:
① defines a class and implements a comparator interface.
② implements an abstract method in an interface compare (E o1,e O2).
③ Instantiate this comparator
④ calls the overloaded method of collections: Sort (Collection c,comparator Comparator).
Case 11:
650) this.width=650; "style=" width:651px;height:391px; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M02/6e/f4/wkiol1wng9uxf7nnaajzknlzplo765.jpg "width=" 681 "height=" 417 "alt=" wkiol1wng9uxf7nnaajzknlzplo765.jpg "/ >
3. The difference between collection and collentions
Collection is the interface under Java.util, it is the parent interface of various collections, and the interfaces that inherit from it are set and list;
Collections is a Java.util class, a helper class for collections that provides a series of static methods for searching, sorting, threading, and so on for various collections.
This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1666023
Java Core API--7 (iterator iterator, comparable, comparator comparator)