Traversal implementation of Map interface

Four traversal methods of Map interface

  1. Using the Set set Set iterator method
    1) First, use the keySet method to get the Set set of the keySet
    2) Then, according to the iterator method of Set set Set, the corresponding iterator is obtained
    3) Traversal according to iterator of Set
    4) The key codes are as follows



Mode 1: iterator while loop

System.out.println("==========Traversal mode 1==========");
		Set keySet = map.keySet();
		Iterator iterator = keySet.iterator();
		while(iterator.hasNext()){
			Object key = iterator.next();
			System.out.println(key+"---"+map.get(key));
		}

Mode 2: enhance for loop

System.out.println("==========Ergodic mode 2==========");
		for (Object key1 : keySet) {
			System.out.println(key1+"---"+map.get(key1));
		}
  1. Using the entrySet iterator method
    1) First, use the entrySet method of Map interface to get a Set
    2) Get a new iterator iterator from the Set set
    3) Traversal by iterator


Mode 3

System.out.println("==========Traversal mode 3==========");
		Set entrySet = map.entrySet();
		Iterator iterator2 = entrySet.iterator();
		while(iterator2.hasNext()){
			Map.Entry node = (Map.Entry)iterator2.next();
			System.out.println(node.getKey()+"---"+node.getValue());
		}

Mode 4

System.out.println("==========Traversal mode 4==========");
		for (Object object : entrySet) {
			Map.Entry node2 = (Map.Entry)object;
			System.out.println(node2.getKey()+"---"+node2.getValue());
		}
  1. Complete code is attached at the end

code

package Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapForeach {
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void main(String[] args) {
		HashMap map = new HashMap();
		Student student1 = new Student("001","Tang Xiaoqiang");
		Student student2 = new Student("002","Tang Daqiang");
		Student student3 = new Student("003","The soup is strong");
		Student student4 = new Student("004","The best soup");
		map.put(student1.getID(), student1);
		map.put(student2.getID(), student2);
		map.put(student3.getID(), student3);
		map.put(student4.getID(), student4);
		
		System.out.println("==========Traversal mode 1==========");
		Set keySet = map.keySet();
		Iterator iterator = keySet.iterator();
		while(iterator.hasNext()){
			Object key = iterator.next();
			System.out.println(key+"---"+map.get(key));
		}
		
		
		System.out.println("==========Ergodic mode 2==========");
		for (Object key1 : keySet) {
			System.out.println(key1+"---"+map.get(key1));
		}
		
		System.out.println("==========Traversal mode 3==========");
		Set entrySet = map.entrySet();
		Iterator iterator2 = entrySet.iterator();
		while(iterator2.hasNext()){
			Map.Entry node = (Map.Entry)iterator2.next();
			System.out.println(node.getKey()+"---"+node.getValue());
		}
		
		System.out.println("==========Traversal mode 4==========");
		for (Object object : entrySet) {
			Map.Entry node2 = (Map.Entry)object;
			System.out.println(node2.getKey()+"---"+node2.getValue());
		}
	}
}


class Student{
	private String name;
	private String ID;
	public Student(String iD, String name ) {
		super();
		this.name = name;
		ID = iD;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getID() {
		return ID;
	}
	public void setID(String iD) {
		ID = iD;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "ID ="+ID+" name="+name;
	}
}

Tags: Java

Posted on Sun, 24 May 2020 10:09:52 -0400 by jocknerd