Collection-Iterator-foreach, iteratorforeach
1. Collection (java. util)
1. Overview: the aggregation of a class of things of the same nature is called a set. Any set contains three pieces of content: Implementation of external interfaces/algorithms for set operations.
Java uses Collection to indicate the top-level interface of a single column set.
Public interface Collection <E> extends Itretaor <E> {}
Note: The interface cannot be directly instantiated, and its subclass is required.
2. Features and system: Collection is a top-level interface that only describes the common content of a Set. Common Sub-interfaces are List and Set.
Common Content:
(1) The length of the collection container can be changed;
(2) data of the same type is stored;
(3) data types can only be stored and referenced (the basic data types must be boxed );
List: ordered/allowed repetition;
Set: unordered/repeated;
Queue: Queue interface;
SortedSet: sorts the data in the set;
3. Common Methods:
Public boolean add (E e) {}: insert an object to the Set;
Public boolean addAll (Collection <? Extends E> c) {}: Insert the content of a set;
Public void clear () {}: clear all content in this collection, but keep the collection container;
Public boolean contains (Object o) {}: determines whether an Object exists in the collection;
Public boolean containsAll (Collection <?> C) {}: determines whether a group of objects exist in the collection;
Public boolean equals (Object o) {}: Object comparison;
Public int hashCode () {}: hash value;
Public boolean isEmpty () {}: determines whether the set is empty;
Public Iterator <E> iterator () {}: instantiate the Iterator interface; return the Iterator instantiation object;
Public boolean remove (Object o) {}: delete a specified Object;
Public boolean removeAll (Collection <?> C) {}: delete a group of objects;
Public boolean retainAll (Collection <?> C) {}: Save the specified content;
Public int size () {}: returns the set size;
Public Object [] toArray () {}: convert a set into an Object array;
Code:
1 import java. util. collection; 2 import java. util. arrayList; 3 4 public class Coll {5 public static void main (String [] args) {6 // instantiate the interface object through subclass, because all interfaces are abstract, 7 Collection <String> c = new ArrayList <String> (); 8 // add element add () 9 c. add ("Rose"); 10 c. add ("Jack"); 11 // view the set content 12 System. out. println (c); // [Rose, Jack] 13 // view the length of the set 14 System. out. println (c. size (); // 2 15 // check whether tom 16 System is included. out. println (c. contains ("tom"); // false 17 // check whether Tom 18 c. add ("Tom"); 19 System. out. println (c. contains (new String ("Tom"); // true -- new String ("Tom") returns a "Tom" String 20 // deletes the specified object remove () 21 c. remove ("Rose"); 22 System. out. println (c); // [Jack, Tom] 23 // delete all objects-retain the set 24 c. clear (); 25 System. out. println (c); // [] 26 // judge whether it is empty 27 System. out. println (c. isEmpty (); // true 28} 29}
Ii. Iterator (java. util)
1. Definition: The Iterator interface must be used to operate the tool class of a specific set as long as the output operation of the set is met.
Pbulic interface Iterator <E>
2. Common Methods:
Public boolean hasNext () {}: determines whether the next value exists;
Public E next () {}: retrieve the current element;
Public void remove () {}: remove the current element;
3. Notes:
(1) The iterator can only delete data, but cannot add data;
(2) When the iterator iterates data, you cannot use the method that comes with the set to change the length of the set! Otherwise, an exception will be reported: ConcurrentModificationException concurrent modification exception!
(3) Pay attention to the specified type (for example, Iterator <String> it = Collection) after the Iterator is built. iterator ();). Otherwise, the Operation will fail. Print only;
1 code 1: // requirement: Use Iterator to output the set content 2 import java. util. collection; 3 import java. util. arrayList; 4 import java. util. iterator; 5 6 public class IteratorDemo {7 public static void main (String [] args) {8 // instantiate Collection object 9 Collection by subclass object <String> c = new ArrayList <String> (); 10 // Add element 11 c to set c. add ("Green"); 12 c. add ("Smith"); 13 c. add ("Philip"); 14 // use the iterator method of Collection to create the iterator object 15 Iterator <String> it = c. iterator (); // note that the type (<String>) is specified when the iterator is built. Otherwise, an error occurs, print only no problem 16 // output all elements in set c 17 while (it. hasNext () {18 System. out. print (it. next () + "\ t"); // Note: We recommend that you call the next () method only once, because next () will also move the Pointer Forward during output, "NoSuchElementException" 19 // System. out. println ("I like" + it. next (); // java. util. noSuchElementException 20} 21} 22}
1 Code 2: // requirement: delete element 2 import java. util. collection; 3 import java. util. arrayList; 4 import java. util. iterator; 5 6 public class IteratorDemo {7 public static void main (String [] args) {8 // instantiate Collection object 9 Collection by subclass object <String> c = new ArrayList <String> (); 10 // Add element 11 c to set c. add ("Green"); 12 c. add ("Smith8"); 13 c. add ("Philip"); 14 // foreach iteration 15 for (String s: c) {16 System. out. print (s + "\ t") // Green Smith8 Philip 17} 18 System. out. println ("============================") 19 // use the iterator method of Collection, create an iterator object 20 Iterator <String> it = c. iterator (); // note that the type (<String>) is specified when the iterator is built. Otherwise, an error occurs, print only OK 21 // output all elements in set c 22 while (it. hasNext () {23 // retrieve the element 24 String str = it in the set. next (); 25 // determines whether the string contains digits-regular (the last digit of the string is a digit) 26 if (str. matches ("\ w + \ d") {27 it. remove (); 28} else {29 System. out. print (str + "\ t"); // Green Philip 30} 31} 32 System. out. print ("delete the name with a digit:" + c + "\ t"); // delete the name with a digit: [Green, philip] 33} 34} 35
3. for -- each
1. Role: enhanced for is a new feature after JDK1.5. It can be used to iterate (traverse/loop) sets or arrays! You can use the enhanced for to replace the iterator to get the element content of the set!
2. format:
For (data type variable name: Object Name or array name ){
Every element in a set or array!
}
3. Code: see section 2. Iterator Code 2.