1. Base and delete functions of map collection
1 Public classDemo1_map {2 3 /*4 * The Map collection is in the form of a key-value pair, and the underlying dependency is the set set5 * 6 * A. Adding features7 * v put (K key, v value)8 * If the key is deposited for the first time, the element is stored directly, returning null (in essence, it is the same as the key exists, only the null is overwritten)9 * If the key is not deposited for the first time, replace the previous value with the value and return the previous valuesTen * b. Remove Features One * void Clear () Removes all key-value information A * V Remove (Object key) removes the element value corresponding to the specified key and returns the value, returning NULL when the passed key value does not exist - * C. Judging function - * Boolean containskey (Object key) to determine if this key is included in the collection the * Boolean containsvalue (Object value) determines whether the collection contains the specified value - * Boolean isEmpty () determines whether the set is empty - * d. Get features - * set<map.entry<k,v>> entryset () + * V get (Object key) Gets the value according to the key - * set<k> KeySet () Gets the collection of all the keys in the collection + * collection<v> values () gets a collection of all the values in the collection and returns a Conllection A * E. Length function at * int size () returns the number of all key-value pairs in the collection - * - */ - Public Static voidMain (string[] args) { - - //demo1 (); in //Demo2 (); - //Demo3 (); tomap<string, integer> map =NewHashmap<>(); +Integer I1 = Map.put ("Zhang San", 23); -Integer i2 = Map.put ("John Doe", 24); theInteger i3 = Map.put ("Harry", 25); *Integer i4 = Map.put ("Zhao Liu", 26); $Integer i5 = map.put ("Zhang San", 26);Panax NotoginsengSet<entry<string, integer>> set =Map.entryset (); -SYSTEM.OUT.PRINTLN (set);//[John Doe = 24, Zhang San = 26, Harry = 25, Zhao Liu =26] theSystem.out.println (Map.values ());//[ +, +, +] + A } the + /** - * Judging function $ */ $ Public Static voidDemo3 () { -map<string, integer> map =NewHashmap<>(); -Map.put ("Lisi", 24); the BooleanB1 = Map.containskey ("Lisi"); -System.out.println (B1);//trueWuyi BooleanB2 = Map.containsvalue (24) ; theSystem.out.println (B2);//true - } Wu - /** About * Delete function $ */ - Public Static voidDemo2 () { -map<string, integer> map =NewHashmap<>(); -Map.put ("John Doe", 24); AInteger i = Map.Remove ("Zhang San"); +System.out.println (i);//NULL theInteger I1 = Map.Remove ("John Doe"); -System.out.println (I1);// - $ } the the /** the * Add features the */ - Public Static voiddemo1 () { inmap<string, integer> map =NewHashmap<>(); theInteger I1 = Map.put ("Zhang San", 23); theInteger i2 = Map.put ("John Doe", 24); AboutInteger i3 = Map.put ("Harry", 25); theInteger i4 = Map.put ("Zhao Liu", 26); theInteger i5 = map.put ("Zhang San", 26); the System.out.println (map); + System.out.println (i1); - System.out.println (i2); the System.out.println (i3);Bayi System.out.println (I4); the System.out.println (i5); the } - -}
2. Traverse the Map Collection
1 Public classDemo2_map {2 3 /**4 * @paramargs5 * Traverse Map Collection6 * 7 */8 Public Static voidMain (string[] args) {9 Tenmap<string, integer> map =NewHashmap<>(); OneMap.put ("Zhang San", 23); AMap.put ("John Doe", 24); -Map.put ("Harry", 25); -Map.put ("Zhao Liu", 26); the - /* - Iterating through the map collection using iterators - set<string> Set = Map.keyset (); + iterator<string> it = Set.iterator (); - While (It.hasnext ()) { + String key = It.next (); A System.out.println (key + "=" + map.get (key)); at }*/ - - - //traversing the map collection with an enhanced for loop - for(String string:map.keySet ()) { -System.out.println (string + "=" +Map.get (String)); in } - } to +}
Map of the Java collection