Package collection;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Set;
public class Mymap {
/**
* @param args
*/
public static void Main (string[] args) {
The first meaning is that value cannot be int, and the following row is an error.
Map<int, int> map=new hashmap<int, int> ();
Try again to find that the key is type int with type value of string and no more, the following line again is an error
Map<int, string> map=new hashmap<int, string> ();
Finally, I think that the key and value of map must be object type (including encapsulation type) The following line is not an error
Map<integer, string> map=new Hashmap<integer, string> ();
There is no error in the following line
Map<user, string> map=new Hashmap<user, string> ();
Map<string, string> map=new hashmap<string, string> ();
Map.put ("1", "Zhangsan");
Map.put ("2", "Lisi");
Map.put ("3", "Wangwu");
The first is to traverse the map
Set<string> set= Map.keyset ()//Get all map key values, stored in set
Map.Remove ("2");/remove key value equal to 2 Lisi the rush is gone.
For (iterator<string> iterator=set.iterator (); Iterator.hasnext ();) {
String Str=iterator.next ();
if (Iterator.hasnext ()) {
System.out.print (Map.get (str) + "");//Print out
}else{
SYSTEM.OUT.PRINTLN (Map.get (str));//Print out
}
Print as Wangwu Zhangsan
}
Second, returns true if the mapping does not contain a key-value mapping relationship.
System.out.println (Map.isempty ())//return false here
Contains a key key
System.out.println (Map.containskey ("1"))//Here returns true to prove the existence of the key
Hash code
System.out.println (Map.hashcode ());//Print 2070547704
When and only if this mapping contains at least one mapping relationship to the value V of the satisfying (Value==null v==null:value.equals (v))
System.out.println (Map.containsvalue ("AA"));//efficiency is not the best. ContainsKey more efficient.
System.out.println (Map.size ())//map two sides and several key value pairs
}
}