Sort out some of the common methods of HashMap according to Java1.6 's API.
1.size
public int size ();
Returns the number of key-value mapping relationships in this map
2.isEmpty
public boolean isEmpty ()
Determine if this map does not contain a key-value mapping relationship
3.get
Public V get (Object key)
Returns the value mapped by the specified key, or returns if the map does not contain any mapping relationships for that key null
. The return null
value does not necessarily indicate that the map does not contain a mapping for the key, or that the map maps the key to a null
.
4.containsKey
public boolean ContainsKey (Object key)
Determines whether the specified key is contained in the map and contains the return true
5.containsValue
public boolean Containsvalue (Object value)
determine if the map contains a specified value
6.put
Public V put (K key, v value);
associates the specified value with the key in this map. If the map previously contained a mapping of the key, the old value is replaced. The return value iswith theKeythe old value of the association;Keydoes not have any mapping relationships, it returnsNULL. (ReturnNULLIt may also indicate that the mapping was preceded by aNULLwith theKeyAssociation. )
7.putAll
public void Putall (map<k,v> m)
Specifies that all mappings of mappings are copied to this map
8.remove
Public V Remove (Object key);
removes the mapping of the specified key (if any) from this map. The return value iswith theKeythe old value of the association;Keydoes not have any mapping relationships, it returnsNULL. (ReturnNULLIt may also indicate that the mapping was preceded by aNULLwith theKeyAssociation. )
9.clear
public void Clear ()
Removes all mapping relationships from this map. When this call returns, map will be empty.
10.keySet
Public set<k> KeySet ()
Returns a set view of the keys contained in this map
11.values
Public collection<v> VALUES ()
Returns a collection view of the values contained by this map
12.entrySet
Public set<entry<k,v>> entryset ();
returns the set of mappings that this mapping contains View
Package Collection;import Java.util.collection;import java.util.hashmap;import java.util.list;import java.util.Map; Import Java.util.map.entry;import Java.util.set;public class Hashmaptest {public static void main (string[] args) {// HASHMAP is the implementation of the Map interface, stores the key-value pairs, and allows null values and NULL keys, this implementation is non-synchronous map<integer, integer> Map = new Hashmap<integer,integer> ( ); Insert some key value pairs for (int i = 0; i < i++) {map.put (i, i + 1); //1. Returns the number of key-value mapping relationships in this map. int size = Map.size (); SYSTEM.OUT.PRINTLN ("Common key-value pairs:" + size); 2. Determine if the map does not contain a key-value pair of Boolean isEmpty = Map.isempty (); SYSTEM.OUT.PRINTLN ("Determine that the map contains key-value pairs:" + isEmpty); 3. Gets the value of the specified key mapping, and returns null int value = Map.get (1) If the mapping does not contain any mapping for the key; System.out.println (the value of the "Key 1 mapping is:" + value); 4. Determine if the map contains the specified key, Boolean isexist = Map.containskey (1); SYSTEM.OUT.PRINTLN ("map contains a mapping relationship of key 1:" + isexist); 5. Determine if the map contains the specified value isexist = Map.containsvalue (-1); SYSTEM.OUT.PRINTLN ("map contains a mapping relationship with a value of-1" + isexist); 6. Associate the specified key and value in the map, if the map previously containedA mapping relationship for the key, the old value is replaced. The return value is the old value associated with the key, or null if the key does not have any mapping relationships. Map.put (11,56); 7. Copy all mappings of the specified map to the current map Map<integer, integer> map2 = new hashmap<integer,integer> (); Map2.put (12, 23); Map.putall (MAP2); 8. Remove the mapping relationship for the specified key map.remove (1); SYSTEM.OUT.PRINTLN ("The mapping relationship for key 1 exists" + map.containskey (1)); 9. Get all the keys in the map set<integer> Set = Map.keyset (); System.out.println ("The key in the map is:"); For (integer integer:set) {System.out.println (integer); }//10. Get all values in map collection<integer> Collection = map.values (); System.out.println ("The key in the map is:"); For (integer integer:collection) {System.out.println (integer); //11. Get all the key values in the map to set<entry<integer,integer>> Set2 = Map.entryset (); for (entry<integer,integer> Entry:set2) {System.out.println ("key" + entry.getkey () + "value" + entry.getvalue ()); }//12. Removes all mapping relationships from this map. When this call returns, map will be empty. Map.clear (); SYSTEM.OUT.PRINTLN (number of mappings in map is: "+ map.size ());}}
Common methods of HashMap available in the Java API