The map interface is used to store element pairs (keys and values)
Methods In the map interface
Equals (Object O)
Hashcode
Clear ()
Remove (Object key)
Put (Object key, object value)
Putall (map t)
Entryset returns the set view containing the ing in the map. Each element in the set is a map. Entry object. You can use the getkey () and getvalue () methods (and the setvalue () method) to access the key elements and value elements of the latter.
Keyset () returns the set view of keys contained in the map. Deleting the elements in the set will also delete the corresponding ing (key and value) in the map)
Values () returns the collection view of values contained in the map. Deleting the elements in the collection will also delete the corresponding ing (key and value) in the map)
Get (Object key) returns the value associated with the specified key
Containskey (Object key) true or false;
Containsvalue (object Value) true or false;
Isempty ()
Size ()
1. hashmap
Hashmap provides all optional ing operations and allows the use of null values and null keys. Apart from non-synchronization and the use of null, The hashmap class is roughly the same as hashtable.
Note that this implementation is not synchronous. If multiple threads access a hash ing at the same time, and at least one thread modifies the ing from the structure, it must maintain external synchronization. This
By synchronizing the objects that encapsulate the ing. If such an object does not exist, use the collections. synchronizedmap method to "Wrap" The ing. It is best to complete this operation at creation to prevent unexpected non-synchronous access to the ing, as shown below:
Map M = collections. synchronizedmap (New hashmap (...));
A: traverse hashtable and use the entryset interface.
Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://www.javaeye.com/topic/421014# ">
- Map <string, integer> map =NewHashmap <string, integer> ();
- Map. Put ("1", 1 );
- Map. Put ("2", 2 );
- Map. Put ("3", 3 );
- Map. Put ("3", 3 );
- Iterator itor = map. entryset (). iterator ();
- While(Itor. hasnext ()){
- Map. Entry <string, integer> entry = (Map. Entry <string, integer>) itor. Next ();
- System. Out. println ("Key =" + entry. getkey (). tostring ());
- System. Out. println ("values =" + entry. getvalue (). tostring ());
- }
Map<String,Integer> map=new HashMap<String,Integer>(); map.put("1", 1); map.put("2", 2); map.put("3", 3); map.put("3", 3); Iterator itor=map.entrySet().iterator(); while(itor.hasNext()){ Map.Entry<String,Integer> entry=(Map.Entry<String,Integer>)itor.next(); System.out.println("key="+entry.getKey().toString()); System.out.println("values="+entry.getValue().toString()); }
B: traverse hashtable and use the keyset and values interfaces.
Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://www.javaeye.com/topic/421014# ">
- Set <string> keys = map. keyset ();
- Iterator itor = keys. iterator ();
- While(Itor. hasnext ()){
- Object key = itor. Next ();
- System. Out. println ("Key =" + key. tostring ());
- System. Out. println ("value =" + map. Get (key). tostring ());
- }
- Collection CO = map. Values ();
- Iterator itor = Co. iterator ();
- While(Itor. hasnext ()){
- System. Out. println ("value =" + itor. Next ());
- }
Set<String> keys=map.keySet(); Iterator itor=keys.iterator(); while(itor.hasNext()){ Object key=itor.next(); System.out.println("key="+key.toString()); System.out.println("value="+map.get(key).toString()); } Collection co=map.values(); Iterator itor=co.iterator(); while(itor.hasNext()){ System.out.println("value="+itor.next()); }
Differences between hashtable and hashmap:
1. hashtable is a subclass of dictionary, and hashmap is an implementation class of the map interface;
2. Methods in hashtable are synchronized, while those in hashmap are not synchronized by default. That is to say, in multi-threaded applications, it is safe to use it without special operations.
Hashtable is used. For hashmap, an additional synchronization mechanism is required. However, the synchronization problem of hashmap can be solved through a static method of collections:
Map collections. synchronizedmap (MAP m)
This method returns a synchronous map, which encapsulates all the underlying hashmap methods, making the underlying hashmap safe even in a multi-threaded environment.
3. In hashmap, null can be used as a key, and there is only one such key. One or more keys can correspond to null values. When the get () method returns NULL, it can represent
If the key is not in hashmap, the corresponding value of the key can also be null. Therefore, the get () method cannot be used in hashmap to determine whether a key exists in hashmap. Instead
The containskey () method.