Map uses key-value pairs to store data, maps keys to value objects, a map cannot contain duplicate keys, and each key can be mapped to at most
A value. Specific implementation classes for map interfaces: Hashmap,hashtable,treemap,linkedhashmap
1) HashMap
A map interface implementation based on a hash table (hash table learning address). Null values and NULL keys are allowed, the order of mappings is not guaranteed, special
Do not make sure that the order is constant. It is roughly the same as Hashtable except for non-synchronous and null-allowed use.
HashMap Implementation principle:
The 1.HASHMAP default array size is 16 and the load factor is 0.75, which needs to be expanded when the array usage reaches the array length * load factor.
2.HashMap is the array as the head, the link list formed (to calculate the hashcode value of key), according to the hash value of the object in the HA
Where the hash table is stored, multiple data at the same location is concatenated in a linked list, and each time an object is first added to the linked list header.
650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20160802/8f355d0b41f24ac5a5eb6e07583733cb_th.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>
Example code:
Java code
Publicstaticvoidmain (string[] args) {
Map<integer, String>maps=newhashmap<integer, string> ();
Maps.put (1, "little Black");
Maps.put (2, "small white");
Maps.put (Null,null);
Maps.put (10, "Lao Wang");
SYSTEM.OUT.PRINTLN ("Size:" +maps.size ());
Get object values by key
System.out.println (Maps.get (10));
Whether this key exists
System.out.println (Maps.containskey (2));
Whether this value exists
SYSTEM.OUT.PRINTLN (Maps.containsvalue (null));
Traverse
Set<entry<integer,string>>entry=maps.entryset ();
Iterator<entry<integer,string>>iter=entry.iterator ();
while (Iter.hasnext ())
{
System.out.println (Iter.next ());
}
Clear Map
Maps.clear ();
}
HashMap is thread insecure, non-synchronous, when a re-hash is needed, the array expands to twice times the original, which will put all the data
Re-insert, this may break the original order, all HashMap cannot guarantee the mapping order. Haspmap's re-hashing will shadow
Sound performance.
2) Hashtable
This class implements a hash table that maps the key to the corresponding value. Any non-null can be used as a key or value in order to successfully
To store and get objects in a hash table, objects used as keys must implement the Hashcode method and the Equals method.
Usage basic and HashMap the same, but more introduction
The difference between HashMap and Hashtable
1.HASHMAP can use NULL as a key or value, Hashtable can not
2.HashMap is non-synchronous, thread insecure, and Hashtable is synchronous, thread-safe
The default capacity of 3.Hashtable is 11, while the default capacity of HASHM is 16.
3) TreeMap
Based on a red-black tree, the map is sorted according to the natural ordering of its keys, or according to the comparator provided when the mapping was created
To sort. Therefore, TreeMap implements the comparable interface when using a custom class as key.
Such as:
Customize the person class to implement the comparable interface. Sorting using the Age property
Java code
Publicclassperson implementscomparable<person>{
Publicstring name;
Publicintage;
Publicperson (String name,intage)
{
This.name=name;
This.age=age;
}
Publicintcompareto (person O) {
if (o==null)
Thrownewnullpointerexception ();
if (this.age>o.age)
RETURN1;
if (this.age<o.age)
return-1;
Return0;
}
@Override
Publicstring toString () {
return "person [name=" + name + ", age=" + Age + "]";
}
}
Calling code:
Java code
Publicstaticvoidmain (string[] args) {
Map<integer, String>maps=newtreemap<integer, string> ();
Maps.put (1, "little Black");
Maps.put (2, "small white");
Maps.put (10, "Lao Wang");
Traverse
Set<entry<integer,string>>entry=maps.entryset ();
Iterator<entry<integer,string>>iter=entry.iterator ();
while (Iter.hasnext ())
{
System.out.println (Iter.next ());
}
Clear Map
Maps.clear ();
-----------------------------Use custom person to do key
Map<person, String>map=newtreemap<person, string> ();
Map.put (Newperson ("Xiaobai"), "Xiaobai");
Map.put (Newperson ("Xiaohei"), "Xiaohei");
Map.put (Newperson ("Xiaohong"), "Xiaohong");
Traverse
Set<entry<person,string>>entry1=map.entryset ();
Iterator<entry<person,string>>iter1=entry1.iterator ();
while (Iter1.hasnext ())
{
System.out.println (Iter1.next ());
}
Clear Map
Map.clear ();
}
Operation Result:
650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20160802/3aaa9a9e940a4530bc498708302648c4.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>
Technology sharing: Edith Academy
This article from the "11247808" blog, reproduced please contact the author!
Map interface of Java Collection