Map Interface
The class that implements the map interface is used to store key-value pairs.
Implementation classes of the map interface include hashmap and treemap.
Key-value pairs stored in the map class are identified by keys. Therefore, key-value pairs cannot be repeated.
Hashmap: The thread is unsafe and efficient. The key or value is allowed to be null.
Hashtable: thread security and low efficiency. The key or value is not allowed to be null.
Properties: subclass of hashtable. Both key and value are strings.
Common Methods:
Object put (Object key, object value );
Object get (Object key );
Object remove (Object key );
Boolean containskey (Object key );
Boolean containsvalue (object value );
Int size ();
Boolean isempty ();
Void putall (MAP t );
Void clear ();
Note: Put usage: When an element is added, when the key value is the same, the added value overwrites the corresponding value of the original key.
And put back the value corresponding to the original key
The first method to traverse the map set
// The first method of proportional hashmap Map <string, string> maps = New Hashmap <string, string> (); Maps. Put ( " A " , " Shang Xiaofei " ); Maps. Put ( " B " , " Shang xiaoshuai " ); Maps. Put ( " C " , " Shang Xiaoyan " ); // Obtains a set of all keys and traverses the Set set to retrieve the value of map. Set <string> Set =Maps. keyset (); For (Iterator = Set . Iterator (); iterator. hasnext ();) {string key = (String) iterator. Next (); system. Out . Println ( " Test. Main () " + " Key --> " + Key + " Value --> " + Maps. Get (Key ));}
View code
Method 2: traverse the map set
// Method 2 for Traversing hashmap Map <string, string> maps = New Hashmap <string, string> (); Maps. Put ( " A " , " Shang Xiaofei " ); Maps. Put ( " B " , " Shang xiaoshuai " ); Maps. Put ( " C " , " Shang Xiaoyan " ); Maps. Put ( " A " , " Sdafasd " ); // Obtains a set of all keys and traverses the Set set to retrieve the value of map. Set <string> Set = Maps. keyset (); For (String: Set ) {System. Out . Println ( " Test. Main () " + Maps. Get ());}
View code
The third method to traverse the map set
// Method 3 for Traversing hashmap Map <string, string> maps = New Hashmap <string, string> (); Maps. Put ( " A " , " Shang Xiaofei " ); Maps. Put ( " B " , " Shang xiaoshuai " ); Maps. Put ( " C " , " Shang Xiaoyan " ); Maps. Put ( " A " , " Sdafasd " ); Set <Entry <string, string> entryset = Maps. entryset (); For (Iterator = Entryset. iterator (); iterator. hasnext ();) {entry <String, string> entry = (Entry <string, string>) Iterator. Next (); system. Out . Println ( " Test. Main () " + Entry. getkey () + " ---- " + Entry. getvalue ());}
View code
Method 4: traverse Map sets
// Method 4 for Traversing hashmap Map <string, string> maps = New Hashmap <string, string> (); Maps. Put ( " A " , " Shang Xiaofei " ); Maps. Put ( " B " , " Shang xiaoshuai " ); Maps. Put ( " C " , " Shang Xiaoyan " ); Maps. Put ( " A " , " Sdafasd " ); Set <String> Set =Maps. keyset (); iterator <String> iterator = Set . Iterator (); While (Iterator. hasnext () {string key = Iterator. Next (); system. Out . Println ( " Test. Main () " + Maps. Get (Key ));}}
View code