17th Chapter Java Container (MAP)

Source: Internet
Author: User

1. The collection of dependent hash tables (HASHSET,LINKEDHASHSET,HASHMAP,LINKEDHASHMAP) is guaranteed to be unique by the hashcode () and Equals () methods

Custom objects to override the Hashcode () and Equals () methods

TreeSet and TreeMap are based on binary tree and red-black tree implementations, implementing comparable or comparator interfaces to sort and ensure uniqueness

1  Packagecn.itcast_02;2 3 Importjava.util.Collection;4 ImportJava.util.HashMap;5 ImportJava.util.Map;6 ImportJava.util.Map.Entry;7 ImportJava.util.Set;8 9 /*Overview of the features of the map collection:Ten * 1. Add function: V put (K key,v value): add Element One * Key does not exist, it is stored directly, return Null;key exists to overwrite the value and return to previous value A * 2. Delete function: void clear (): Remove all key-value pair elements - * V Remove (Object key): Deletes the key value pair element according to the key, and returns the value - * 3. Function: Boolean containskey (Object key): Determines whether the collection contains the specified key the * Boolean containsvalue (Object value): Determines whether the collection contains the specified value - * Boolean isEmpty (): Determines whether the collection is empty - * 4. Get function:set<map.entry<k,v>> entryset ():??? - * V get (Object key): Gets the value according to the key + * set<k> keySet (): Gets the collection of all the keys in the collection - * collection<v> values (): Gets the collection of all values in the collection + * 5. Length function: int size (): Returns the logarithm of the key-value pairs in the collection A  *  at  */ -  Public classMapdemo { -      Public Static voidMain (string[] args) { -         /*Map<integer, string> Map = new hashmap<> (); - //Add element - System.out.println (Map.put (1, "Hello"));//null in System.out.println (Map.put (1, "World");//hello - //removing elements to System.out.println (Map.Remove (1));//world + */         -map<string, string> map =NewHashmap<>(); theMap.put ("Zhang San", "AAA"); *Map.put ("Zhang San", "AAA"); $Map.put ("John Doe", "BBB");Panax NotoginsengMap.put ("Harry", "CCC"); -Map.put ("Caifan", "ddd"); theMap.put ("Red Flag", "Eee"); +         //get the value by key (V get (Object key)) A         /*System.out.println (Map.get ("Zhang San"));//aaa the System.out.println (Map.get ("John Doe"));//BBB + */        //gets the collection of all keys (set<k> keySet ()) -Set<string> set =Map.keyset (); $          for(String s:set) { $             //System.out.println (s); - System.out.println (Map.get (s)); -         } theSystem.out.println ("-----------"); -         //get a collection of all valuesWuyicollection<string> con =map.values (); the          for(String string:con) { - System.out.println (string); Wu         } -System.out.println ("---------"); About         //gets the collection of objects for all key-value pairs $set<map.entry<string, string>> entries =Map.entryset (); -          for(Entry<string, string>entry:entries) { -String key =Entry.getkey (); -String value =Entry.getvalue (); ASystem.out.println (key+ "--" +value); +         } the     } -}

2.linkedhashmap<k,v> is a hash table and a linked list implementation of the map interface, the hash table guarantees that the key is unique, the list is guaranteed access consistent, the key is a custom object to be rewritten

Hashcode () and Equals () methods

3.TREEMAP is the implementation of map interface based on red and black tree

1  Packagecn.itcast_04;2 3 ImportJava.util.Set;4 ImportJava.util.TreeMap;5 6 /*7 * TreeMap: is based on the red-black tree map interface implementation. 8  * 9 * hashmap<string,string>Ten * Key: String One * Value: String A  */ -  Public classTreemapdemo { -      Public Static voidMain (string[] args) { the         //To create a collection object -treemap<string, String> TM =NewTreemap<string, string>(); -  -         //creating elements and adding elements +Tm.put ("Hello", "Hello"); -Tm.put ("World", "worlds"); +Tm.put ("Java", "Javanese"); ATm.put ("World", "2"); atTm.put ("Java ee", "java ee"); -  -         //iterating through the collection -Set<string> set =Tm.keyset (); -          for(String key:set) { -String value =Tm.get (key); inSYSTEM.OUT.PRINTLN (key + "---" +value); -         } to     } +}
1  Packagecn.itcast_04;2 3 ImportJava.util.Comparator;4 ImportJava.util.Set;5 ImportJava.util.TreeMap;6 7 /*8 * treemap<student,string>9 * Key: StudentTen * Value: String One  */ A  Public classTreeMapDemo2 { -      Public Static voidMain (string[] args) { -         //To create a collection object theTreemap<student, String> TM =NewTreemap<student, string>( -                 NewComparator<student>() { - @Override -                      Public intCompare (Student s1, Student S2) { +                         //Main conditions -                         intnum = S1.getage ()-s2.getage (); +                         //Secondary Condition A                         intnum2 = num = = 0?s1.getname (). CompareTo ( at S2.getname ()): num; -                         returnnum2; -                     } -                 }); -  -         //Create student Objects inStudent S1 =NewStudent ("Koh Phangan", 30); -Student s2 =NewStudent ("Willow Xia Hui", 35); toStudent s3 =NewStudent ("Tong Pak Fu", 33); +Student S4 =NewStudent ("燕青", 32); -Student S5 =NewStudent ("Tong Pak Fu", 33); the  *         //Storage Elements $Tm.put (S1, "Song Dynasty");Panax NotoginsengTm.put (S2, "Yuan Dynasty"); -Tm.put (S3, "Ming Dynasty"); theTm.put (S4, "Qing Dynasty"); +Tm.put (S5, "Han Dynasty"); A  the         //Traverse +Set<student> set =Tm.keyset (); -          for(Student key:set) { $String value =Tm.get (key); $System.out.println (Key.getname () + "---" + key.getage () + "---" -+value); -         } the     } -}

17th Chapter Java Container (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.