Detailed explanation of java iterator

Export of iterators

Before the jdk1.5 version, there was no foreach. However, the 1.5 version added foreach, and the new foreach function introduced was not improved on the jvm because the cost was too high. Oracle engineers thought of a better way is Introduce the concept of iterator in the compiler to realize foreach traversal.

Please see a piece of code:

// The function uses HashSet to store, delete, and traverse the information of several students
		HashSet<Student> hash = new HashSet<Student>();
		hash.add(new Student("张三", 20));
		hash.add(new Student("李四", 21));
		hash.add(new Student("王二", 22));
		hash.add(new Student("麻子", 23));
		
		String st1 = "hello";
		String st2 = "hello";
		String st3 = st2;
		System.out.println(st1 == st3);
		
// delete a student named Zhang San
		String name1 = "Zhang San";
		boolean flag = true;
		for (Student student : hash) {
			String name2 = student.getName();
			
			if(name1 == name2){
				hash.remove(student);
				flag = false;
				break;
			}
		}
		if(flag){
			System.out.println("Does not exist");
		}
		
		for (Student s : hash) {
			System.out.println(s);
		}

 The following error exception occurs when the above code is run multiple times:

This error is to delete the data in the collection while traversing the collection, this writing method does not guarantee thread safety

How to deal with it? The above way of writing is that different people do different things. The solution is to let one person traverse and delete, which leads to iterators.

Let's take a look at the simple usage of iterators:

I first compare the iterator to the tour guide collection that stores all attractions

Please see that the red arrows above appear from the very beginning and then go down one by one through the next() of the iterator  

// usage of iterator
		
// 1. Create a collection
		ArrayList<String> al = new ArrayList<String>();
		al.add("Zhang San");
		al.add("Li Si");
		al.add("王二");
		
// Create an iterator object Note that the iterator is not created with new but with the method of collection
		
		Iterator<String> it = al.iterator();
		
		while(it.hasNext()){
			String s = it.next();
			System.out.println(s);
		}

 Rewrite the writing after the exception:

// The function uses HashSet to store, delete, and traverse the information of several students
		HashSet<Student> hash = new HashSet<Student>();
		hash.add(new Student("张三", 20));
		hash.add(new Student("李四", 21));
		hash.add(new Student("王二", 22));
		hash.add(new Student("麻子", 23));
		
		String st1 = "hello";
		String st2 = "hello";
		String st3 = st2;
		System.out.println(st1 == st3);
		
// delete a student named Zhang San
		String name1 = "Zhang San";
		boolean flag = true;

		Iterator<Student> it = hash.iterator();
		while(it.hasNext()){
			Student s = it.next();
			if(name1.equals(s.getName())){
				it.remove();
				flag = false;
				break;
			}
		}
		if(flag){
			System.out.println("Does not exist");
		}
		
		for (Student s : hash) {
			System.out.println(s);
		}

 The underlying principle of iterators

According to the figure, the unity of the method is achieved. All collections implement the Interable interface and create and return an Iterator object by implementing the interface method.

Then by creating an inner class to implement all the methods in Iterator The ultimate goal is to unify the method names in all collections

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324770816&siteId=291194637