Today's explanation is mainly to use a variety of ways to implement traversal HashMap to take out the Key and value. First of all, if you want a collection to be able to use for enhancement to implement iteration in Java, then this interface or class must implement the Iterable interface. Then how Iterable implements iteration will not be explained here. The following will mainly explain the traversal process.
//Define a generic collection Map<String, String> map = new HashMap<String, String>();//Add data to the collection through the Map put method map.put("001", "Liu Bei"); map.put("002", "Cao Cao"); map.put("003", "Sun Quan");Method 1: Use the KeySet method of the Map interface to implement it
Question: We all know that the Map interface does not implement the Iterable interface, so why can iterate with its KetSet method?
Analysis: Because the KeySet method returns the Set view of the key contained in this map, this method can actually return a Set attempt, which means that its return value type is a Set interface. We can see through the API document that the Set interface implements the Iterable interface, so it can achieve iteration.
//Calling the KeySet method to put back a Set interface type Set<String> set = map.keySet(); //Use for enhancement to get out the key and value for (String item : set) { System.out.println("Key is: " + item + ";Value value is: " + map.get(item)); }Method 2: Use the Values method of the Map interface to implement (for enhancement)
Similarly: when calling the values method of the Map interface, he put back a Collection attempt, and when the Collection interface is implemented, iterable, so it can be iterated.
Collection<String> con = map.values(); for (String item : con) { System.out.println("Value value is: " + item); }Method 3: Use the entrySet method of the Map interface to implement (for enhancement)
entrySet: The return value of entrySet() also returns a Set collection. The type of this collection is Map.Entry. Map.Entry is an internal interface declared by Map. This interface is a generic and is defined as Entry<K,V>. It represents an entity in the Map (a key-value pair).
Set<Entry<String, String>> setentry = map.entrySet(); for (Entry<String, String> item : setentry) { System.out.println("Key is: " + item.getKey() + ";Value value is: " + item.getValue()); }Method 4: Use the keySet().Iterable()(while loop) of the Map interface
Iterable(): Returns an iterator that iterates on the element in this set. The returned elements have no specific order (unless this set is an instance of a class that provides order guarantees). Return value type Iterator<E>
Iterator<String> it = map.keySet().iterator();//Return true if there are still elements that can be iterated. while (it.hasNext()) { //Get the Key value String key = it.next(); System.out.println("Key is: " + key + ";Value is: " + map.get(key)); } Method 5: Use the value.Iterable() of the Map interface (while loop)
Iterator<String> it1 = map.values().iterator(); while (it1.hasNext()) { String value = it1.next(); System.out.println("Value value is: " + value); } Method 6: Use entrySet().Iterable()(while loop) of the Map interface
Iterator<Entry<String, String>> it2 = map.entrySet().iterator(); while (it2.hasNext()) { Entry<String,String> entry=it2.next(); System.out.println("Key is: " + entry.getKey() + ";Value value is: " + entry.getValue()); }The above article uses various methods to traverse HashMap. This is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.