Today's main explanation is to use a variety of ways to implement traversal hashmap to remove key and value, first in Java if you want a collection to be able to use for enhancements to implement the iteration, then this interface or class must implement the Iterable interface, So iterable is how to achieve the iteration, here will not do the explanation, the following main explain the traversal process.
Defines a generic set
map<string, string> Map = new hashmap<string, string> ();
Add data to the collection through the put method of the map
map.put ("001", "Liu Bei");
Map.put ("002", "Caocao");
Map.put ("003", "Sun Quan");
Mode one: Using the keyset method of the map interface to implement
Question: We all know that the map interface does not implement the Iterable interface, why is it possible to use his Ketset method to implement the iteration?
Resolution: Because the Keyset method returns a set view of the keys contained in this map, this method can return a set attempt, and it says that his return value type is a set interface, and we can see the set interface through the API document, and he implements the Iterable interface, so it can implement iterations.
Call the keyset method to put back a set interface type
set<string> set = Map.keyset ();
Use for enhancement to remove key and value for
(String item:set) {
System.out.println ("key is:" + item +); Value is: "+ map.get (item));
}
Mode two: Implementing the values method using the Map interface (for enhancements)
Similarly: The values method that invokes the map interface he put back a collection attempt, collection interface He also implemented the Iterable interface, so can iterate.
collection<string> con = map.values ();
for (String Item:con) {
System.out.println ("value is:" + Item);
}
Mode III: Implementation using the EntrySet method of the map interface (for enhancements)
The return value of the Entryset:entryset () also returns a set set, the type of which is map.entry,map.entry is an internal interface of the Map declaration, and this interface is generic, defined as entry<k,v>. It represents an entity (a Key-value pair) in a map.
set<entry<string, string>> setentry = Map.entryset ();
For (entry<string, string> item:setentry) {
System.out.println ("key is:" + item.getkey () +); Value is: "
+ item.getvalue ());
}
Mode four: Keyset () using the map interface. Iterable () (While loop)
Iterable (): Returns an iterator that iterates over the elements in this set. The returned element is not in a specific order (unless this set is an instance of a class that provides an order guarantee). return value type Iterator<e>
Iterator<string> it = Map.keyset (). iterator ();
Returns true if there are still elements that can be iterated. While (It.hasnext ()) { //Get key value
String key = It.next (); System.out.println ("Key" is: "+ key +"; Value is: "+ map.get (key));
}
Mode five: Value.iterable () with the map interface (while loop)
Iterator<string> it1 = Map.values (). iterator ();
while (It1.hasnext ()) {
String value = It1.next ();
System.out.println ("value is:" + value);
}
Mode VI: EntrySet () using the map interface. Iterable () (While loop)
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 is: "+ entry.getvalue ());
}
The above method of using a variety of ways to achieve traversal HashMap is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.