The map in Java basically uses those methods (tested by hand)

Source: Internet
Author: User

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
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.