Java SE 049 Map.Entry detailed explanation and operation requirements

(1) As long as a person does not give up on himself, the whole world will not give up on you.
(2) I am born to be of great use . (3) If I
cannot bear the suffering of learning, I must bear the suffering of life. How painful it is Deep comprehension.
(4) You must gain from doing difficult things . (
5) Spirit is the real blade.
(6) Conquering opponents twice, the first time in the heart.
(7) Writing is really not easy. If you like it or have something for you Help remember to like + follow or favorite~

Java SE 049 Map.Entry detailed explanation and operation requirements

1. The second method of traversing HashMap

package com.javase.map;

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

public class MapTest5 {
    
    
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
    
    
		HashMap map = new HashMap();
		map.put("a", "aa");
		map.put("b", "bb");
		map.put("c", "cc");
		map.put("d", "dd");
		map.put("e", "ee");
		
		Set set = map.entrySet();
		//明确set里面存和的数据是什么类型的。 
		for(Iterator iter = set.iterator(); iter.hasNext();){
    
    
			Map.Entry entry = (Map.Entry)iter.next();
			
			String key = (String)entry.getKey();
			String value = (String)entry.getValue();
			
			System.out.println(key + ":" +value);
		}
	}
}

This way is faster and simpler.
Map is the mapping information between key and value. For key and value, it is put in, not stored separately. Once the key and value are placed in the value, they are actually the same as the LinkedList. The bottom layer will also generate an object such as Entry. Therefore, if you obtain an Entry object, you can get the key information and at the same time. Value information.

Guess you like

Origin blog.csdn.net/xiogjie_67/article/details/108540825