About the Map collection

Source: Internet
Author: User

The map interface implements the collection interface and is one of the three main interfaces of the set.

The map interface is declared: public interface map<k,v>; maps the key to a pair of values , a map cannot contain duplicate keys, each key can only be mapped to one value, and if a key is mapped to multiple values, The value of the map before it will be overwritten by the value of the mapping, the implementation class of the mapping relationship is mainly Hasgmap and TreeMap class, HashMap mapping implementation does not save order, treemap mapping implementation, can clearly guarantee its order.

1.HashMap

HashMap statement:

1  Public class Hashmap<k,v>2     extends abstractmap<k,v>3     implements Map <k,v>, Cloneable, Serializable
View Code

The HashMap class is based on a hash table (array and linked list) map interface implementation, allowing the use of NULL keys and Mull values, non-synchronous, thread insecure, which is roughly the same as Hashtable, this class does not guarantee the order of mappings, and the order cannot be constant. Its default construction method constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75), that is, when the array capacity reaches 75% of the total capacity and the array is to be expanded, the hash table is rehash operation. That is, rebuilding the internal data structure (hash table hashes). When a hash table holds an object, the total capacity of the HashMap is calculated based on the hashcode of the key (K) object, to determine where the array exists for the mapping relationship that the key-value pair represents, and then the other mappings at that location are stored as a linked list.

1  Packagecom.test;2 3 ImportJava.util.HashMap;4 ImportJava.util.Map;5 ImportJava.util.Set;6 7  Public classHashmapdemo {8      Public Static voidHashMap () {9Map<integer, string> HashMap =NewHashmap<>();TenHashmap.put (1, "string1"); OneHashmap.put (2, "string2"); AHashmap.put (3, "String3"); -Hashmap.put (3, "String4");//Note the overwrite of the mapping relationship under the same key value -Hashmap.put (NULL, "String3"); theHashmap.put (NULL, "String4"); -         /* - * Null as a key, continue to keep the key unique (only once in the key), if the key has more than one map value, the last map value overrides the previous mapping value - * NULL as a value is unique to the key and can be used multiple times +          */ -SYSTEM.OUT.PRINTLN ("Null is the key when the value is obtained:" +hashmap.get (NULL)); +Hashmap.put (4,NULL);//associative key-value pairs AHashmap.put (5,NULL); atSYSTEM.OUT.PRINTLN ("Null is returned as a value:" +hashmap.get (5)); -         /* - * HashMap Traversal - * KeySet () returns the set view of all the keys contained in this map -          */ -SYSTEM.OUT.PRINTLN ("******hashmap traversal ******"); inSet<integer> h =Hashmap.keyset (); -          for(Integer i:h) { to System.out.println (Hashmap.get (i)); +         } -     } the      Public Static voidMain (string[] args) { * HashMap (); $     }Panax Notoginseng}
View Code

Operation Result:

1 the value obtained when NULL is a key: String4 2 NULL is returned as a value:null3 ******hashmap traversal ******4string45  String16string27string48null9 NULL
View Code

2.Hashtable

Hashtable statement:

1  Public class Hashtable<k,v>2extends dictionary<k,v>3implements map<k,v> , Cloneable, Serializable
View Code
This class implements a hash table that maps the keys to the corresponding values. Any non-null object can be used as a key or value, synchronous, thread-safe, key-unique, and the assignment following the mapping of the same key value overrides the value corresponding to the previous key, and the other is similar to HashMap.
1  Packagecom.test;2 3 Importjava.util.Hashtable;4 ImportJava.util.Set;5 /*6 * Hashtable similar to HashMap7 * Thread safety, key value cannot use null value on either side8  */9  Public classHashtabledemo {Ten      Public Static voidMain (string[] args) { OneHashtable<string,string> Hashtable =NewHashtable<>(); AHashtable.put ("1", "manu1"); -Hashtable.put ("2", "MANU2"); -Hashtable.put ("3", "MANU3"); the //hashtable.put (null, "MANU3");//Error - //hashtable.put ("4", null); -Hashtable.put ("3", "Manu5"); -Set<string> set =Hashtable.keyset (); +          for(String s:set) { - System.out.println (Hashtable.get (s)); +         } A     } at } - Operation Result: - Manu5 - manu2 -Manu1
View Code

3.TreeMap

Declaration of the TreeMap class:

1  Public class Treemap<k,v>2extends abstractmap<k,v>3implements Navigablemap <k,v>, Cloneable, Serializable
View Code

TreeMap is based on a red-black tree, which is a balanced binary tree that is sorted according to the natural order of its keys, or sorted according to the Comparator provided when the mapping is created, depending on the construction method used, non-synchronous, thread insecure, and slightly slower than the hashmap rate.

1  Packagecom.test;2 3 ImportJava.util.Iterator;4 ImportJava.util.Map.Entry;5 ImportJava.util.TreeMap;6 7 /*8 * Based on red and black trees, also known as Balanced binary tree9 * The comparator interface must be implemented when customizing objects as keysTen * sorted in natural order or according to CompareTo method One * The uniqueness of the key A  */ -  Public classTreemapdemo { -      Public Static voidMain (string[] args) { theTreemap<string,test> TreeMap =NewTreemap<>(); -Test T1 =NewTest (1, "manu1", 10); -Test t2 =NewTest (2, "manu2", 20); -Test t3 =NewTest (3, "Manu3", 30); +Treemap.put ("a", T1); -Treemap.put ("A", T2);//the keys for key-value pairs in map are unique, overwriting the previous +Treemap.put ("B", T3); ASystem.out.println (Treemap.get ("a")); atSystem.out.println (Treemap.get ("B")); -         /* - * EntrySet () - * Returns a set view of the mappings contained in this map, which is the collection of the object that gets the key value pair - * and Keyset () returns the set view of the key contained in this map, which is to get the K collection in the collection -          */ inSystem.out.println ("******iterator******"); -iterator<entry<string, test>> i =Treemap.entryset (). iterator (); to          while(I.hasnext ()) { + System.out.println (I.next ()); -         } the     } *  $ }Panax Notoginseng Operation Result: -id=2, NAME=MANU2, age=20 theId=3, NAME=MANU3, age=30 +iterator****** Aa=id=2, NAME=MANU2, age=20 theB=id=3, NAME=MANU3, age=30
View Code

About the Map collection

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.