Map and its sub-classes

Source: Internet
Author: User
Tags set set

 Packagecom. Map;Importjava.util.Collection;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportJava.util.Set;/* * Introduce map * We know that the collection is for storing objects. The collection collection is characterized by storing objects in which each value is an object * when encountering this situation: * For example: Students in the school, how to query it? According to the number of students * if you are using collection saved data, you need to traverse collection all students, and then find the corresponding student's content * If you are using a map collection, you can specifically put the student ID, because it is unique, as a key, Then iterate through all the keys, find the * corresponding key, and then indicate the student information corresponding to the key. * I feel that using map to store data simplifies query time.      * * map<k,v> Mapping Entries (key value pairs) * Key is not repeatable, the data structure of map is only related to key, value is stored in the key * * Map<k,v> method * * * Object put (key,value)//returns the original value in the key * Putall (MAP&LT;? extends K,? extends v> m) add all the mappings in M to this map, and if there is a key, replace the * Delete: * Clear () * Remove (object key)//delete the key value of the specified key pair * judgment: * ContainsKey (Object key) * Containsvalue ( Object key) * Equals (Object o) * hashcode () * ISEMPTY () * Get: * set<map.entry<k,v>>      EntrySet () returns the set view of the mappings contained in this map * get (Object key) Gets the property value of the corresponding key in the map * set<k> KeySet () returns the set set of keys in this map *  Collection<v> values () returns the Collection view of the values contained in this map * size () returns the number of key-value mappings in this map*/ Public classStudy01 { Public Static voidMain (string[] args) {Map<Integer,String> map=NewHashmap<integer,string>(); Map<Integer,String> map1=NewHashmap<integer,string>(); //put (k,v)System.out.println ("Put:" +map.put (1, "Guodaxia")); Map.put (1, "Guoxiaoxia"); Map1.put (1, "Aao"); Map1.put (2, "Qaq");//Qaq cry, sell Moe//PutallMap.putall (MAP1); SYSTEM.OUT.PRINTLN (map);//overridden the ToString//Remove (Object key)//System.out.println ("Remove:" +map.remove (1));//System.out.println (map); //Clear ()//map.clear ();//System.out.println (map);System.out.println (Map.containskey (1)); System.out.println (Map.containsvalue ("Qaq")); //get (Key)System.out.println (Map.get (1));; //KeySet ()Set<integer> keys=Map.keyset ();  for(intI:keys) {System.out.println (i+"---"+Map.get (i)); }        //values ()Collection<string> l=map.values ();  for(Iterator<string> it=l.iterator (); It.hasnext ();) {System.out.println (It.next ()); }                //set<map.entry<k,v>> EntrySet () returns a Set view of the mappings contained in this map//map.entry<k,v> is an interface whose implementation class stores a pair of mapped values Key--value, which you understand as an object that stores key-value pairs//several of its methods://GetKey ()//GetValue ()//toString ()//SetValue ()//hashcode ()//equals ()//It is similar to an iterator in collection and can be described as a unique iterator to the map collection, so you can also modify the contents of the collection by thisSet<entry<integer,string>> s=Map.entryset ();  for(entry<integer,string>ss:s) {System.out.println (Ss.getkey ()+ "\ T" +Ss.getvalue ()); }         for(entry<integer,string>ss:s) {            if(Ss.getkey () ==2) {Ss.setvalue ("Haha");            }} System.out.println (map); }} Packagecom. Map;/** * HASHMAP HASH Table structure * * Linkedhashmap bottom is hash table and linked list, ordered unique * * TREEMAP * key is red black tree structure has a specific order, unique * sort and unique with sort method * Natural sort is entity class implements the comparable interface * Sequencer implementation. * To create the sequencer, the sequencer is a class that implements the comparator interface * When the TREEMAP is created, the constructor is used to enter the sequencer*/ Public classStudy02 {}
View Code

Map and its sub-classes

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.