Java Basic Note--map

Source: Internet
Author: User

In the Java world, all things are objects, then map is an interface! haha

As an example:

map<user,string> map=New Hashmap<user, string> ();

where the user class

 PackageCom.map; Public classUser {Private intAge ; PrivateString name;  Public intGetage () {returnAge ; }         PublicUser () {Super(); }        /**     *      * @paramAge *@paramname*/     PublicUser (intage,string name) {        Super();  This. age=Age ;  This. name=name; }         Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Override Public inthashcode () {System.out.println ("Running the Hashcode"); Final intPrime = 31; intresult = 1; Result= Prime * result +Age ; Result= Prime * result + ((name = =NULL) ? 0: Name.hashcode ()); returnresult; } @Override Public Booleanequals (Object obj) {if( This==obj)return true; if(obj = =NULL)            return false; if(GetClass ()! =Obj.getclass ())return false; User Other=(User) obj; if(Age! =other.age)return false; if(Name = =NULL) {            if(Other.name! =NULL)                return false; } Else if(!name.equals (other.name))return false; return true; }            }

Run

 PackageCom.map;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map; Public classMaptest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubUser user=NewUser (10, "Xiaoming"); User User1=NewUser (12, "Xiao Li"); Map<User,String> map=NewHashmap<user, string>(); Map.put (User,"Xiao Ming"); Map.put (User1,"KK"); Iterator<User> iter=Map.keyset (). iterator ();  while(Iter.hasnext ()) {User User2=Iter.next (); String name=Map.get (User2); }                            }}

Get output

Ran the hashcode ran the hashcode

Hashmap--hash is a method that every object has.

The default length of the map is 16, and the hash value of the key gets its position. If the value is the same, it is placed behind the previous value and is a linked list structure

Using debug mode, selecting "View" (watch) will get

Map has a number of implementation classes, Concurrenthashmap, Hashtable, HashMap,

Reference: http://blog.csdn.net/renfufei/article/details/17287729

Java Basic Note--map

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.