1. Map Collection
features : Mapping keys to values of objects, a map cannot contain duplicate keys; Each key can be mapped to a maximum of one value. Disordered.
the difference between a Map and a collection collection :
The Map collection stores elements in pairs, two columns, and collection is a single column
The key to the map is unique, and the Collection sub-system set is unique.
The data structure of the map collection is valid for the key, independent of the value
The data structure of the collection collection is only valid for elements
Features Overview:
1. Adding features
V Put (K key, V value): Add (Modify) function: If the key already exists, replace the value, return the value corresponding to the previous key, store it for the first time, return null
2. Delete function:
void Clear (): Removes all key-value pair elements
V Remove (Object key): Deletes the element based on the key value pair and returns the value
3. Judging function
Boolean ContainsKey (Object key): Determines whether the collection contains the specified key
Boolean Containsvalue (Object value): Determines whether the collection contains the specified value
Boolean IsEmpty (): Determines whether the collection is empty
4. Get Features
Set<map,entry<k,v>> EntrySet (): Returns the key-value pair object
V get (Object key): Gets the value based on the key
Set<e> KeySet (): Gets the collection of all the keys in the collection
Collection<v> values (): Gets a collection of all values in the collection
5. Length function
int size (): Returns the number of collection key-value pairs
Test:
Public Static voidMain (string[] args) {Map<string, string> map =NewHashmap<string, string>(); System.out.println ("Put:" + map.put ("article", "Horse Erie"))); System.out.println ("Put:" + map.put ("article", "Rotten"));//if the key already exists, the value is modified and the value corresponding to the previous key is returnedMap.put ("Wangwang", "Xiaojingzi"); Map.put ("Wangzi", "Xiaojunzi"); Map.put ("Hage", "Xiaoha"); System.out.println ("Map:" +map); System.out.println (Map.Remove ("Wangwang"));//the value that corresponds to the return key: "Xiaojingzi"System.out.println (Map.containskey ("Wangzi"));//there is a return of true, there is no return falseSystem.out.println (Map.size ()); System.out.println (Map.get ("article"));//Returns a value based on the key, no return null existsSet<String> set = Map.keyset ();//returns the combination of all the keys in the collection for(String key:set) {System.out.println (key); } Collection<String> con = map.values ();//returns a collection of all values in the collection for(String Value:con) {System.out.println (value); } for(String Key:set) {//How to traverse the Map collection 1SYSTEM.OUT.PRINTLN (key + "=" +Map.get (key)); } Set<map.entry<string, string>> Set1 = Map.entryset ();//How to traverse the Map collection 2 for(Map.entry<string, string>Me:set1) {System.out.println (Me.getkey ()+ ":" +Me.getvalue ()); } }
2. HashMap class
Features: Key is the structure of the hash table, can guarantee the uniqueness of the key
Java Collection Map