iterator
Iterator
Syntax format:
Iterator it = collection object iterator();
In a collection, simply call the collection object's own iterator() to create an iterator for the collection
Three methods in iterators:
- Boolean hasnext() determines whether there are elements in the next bit of the current cursor. If there are elements, it returns true; if not, it returns false
- E next() moves the iterator cursor down one bit and returns this data
- remove() deletes the element indicated by the current cursor
In principle, the above three methods must be transferred in this order, because this is the essence of a system
Collection c =new ArrayList(); c.add(1); c.add("sdahf"); c.add(true); c.add(3); c.add(2.34); //Create iterator Iterator it =c.iterator(); //If data is added, the iterator will not report an error c.add(4); //Regenerate iterator it =c.iterator(); //Rely on iterator traversal while(it.hasNext()) { Object o =it.next(); System.out.println(o); }
be careful:
- Once an iterator is created, elements cannot be added or deleted from the collection. If elements are deleted or added, the iterator must be regenerated; No need to change data
- The cursor (pointer) will not return to the starting position after the iterator is traversed. If you want to traverse again, you need to regenerate the iterator
- When using an iterator, the deleted element can only be deleted by the method inside the iterator (not by the remove of the collection)
Iterator it = c.iterator(); //Iterator traversal while (it.hasNext()) { Object object = it.next(); // The deletion of a collection cannot be used. After the iterator is created, the collection cannot add or delete elements // c.remove(object); // You can use the deletion of the iterator, which will delete both the iterator and the in the collection // Delete the currently pointed element it.remove(); }
comparator
To sort a collection, you must use a comparator
classification
Element self comparator
Comparable, the added elements need to implement this interface. In this case, it is generally used in our custom classes. For example, our packaging classes of eight basic data types implement this interface, so the elements placed in the collection can be sorted directly.
Sorting of eight basic types of wrapper classes in list
// sort Collections.sort(Collection object name);
For non eight basic data types (not just list), if you add the element's own comparator, you must implement the Comparable interface.
Comparator class
Comparator is often used to change the original sorting. It can cover the element itself.
There are two ways to write it. One is to reset a class outside, and the other is to write an anonymous inner class inside. An external class can be reused, while an anonymous internal class can only be used once for the current. Other cannot be called.
For example, Integer is the default ascending order. If I want to descending order, I need to use Comparator to reorder, because it has high priority.
Writing method of external comparator class:
public static void main(String[] args) { // SortedSet products = new TreeSet(new productComparator()); } //class productComparator implements Comparator { // // @Override // public int compare(Object arg0, Object arg1) { // //arg0 is the element to add // //arg1 is an element in the collection // double price1 = ((Product) arg0).price; // double price2 = ((Product) arg1).price; // if (price1 == price2) { // return 0; // } else if (price1 > price2) { // return 1; // } else { // return -1; // } // } // //}
Anonymous inner class writing method:
public static void main(String[] args) { SortedSet products = new TreeSet(new Comparator() { @Override public int compare(Object o1, Object o2) { // o1 is the element to add // o2 is the element in the set double price1 = ((Product) o1).price; double price2 = ((Product) o2).price; if (price1 == price2) { return 0; } else if (price1 > price2) { return 1; } else { return -1; } } }); }