Collection single-row collection map double-column collection

Source: Internet
Author: User
Tags set set

Map Collection: This collection stores key-value pairs, one-to-one, to-go. and to guarantee the uniqueness of the key.
1. Add
Put (K key, V value)
Putall (map<? extends K,? extends v> m)
2. Delete
Clear ()
Remove (Object key)
3. Judgment
ContainsKey (Object key)
Containsvalue (Object value)
IsEmpty ()
4. Get
Get (Object key): Get value
Size ()
VALUES ()
EntrySet ()
KeySet ()

Map
!--Hashtable: The underlying is a hash table data structure that cannot be stored in a null key null value, which is thread-synchronized. JDK1.0, low efficiency.
!--HashMap: The underlying is a hash table data structure that allows NULL keys and null values that are not synchronized. JDK1.2 high efficiency.
!--TREEMAP: The bottom layer is a two-fork tree data structure, threads are out of sync and can be used to sort keys in the map collection.
It's like the set.
In fact, the set bottom is the use of the map collection.

 Public classMapdemo { Public Static voidMain (string[] args) {Map<String,String> map =NewHashmap<string,string>(); //add element, if the same key appears when added, then the added value will overwrite the original key corresponding value,//The Put method returns the overridden value. System.out.println ("Put:" +map.put ("the", "Zhangsan1")); System.out.println (Put: "+map.put", "Wangwu")); Map.put ("Zhangsan2", "the"); Map.put ("Zhangsan3", "the"); System.out.println ("Containkey:" +map.containskey ("022")); //System.out.println ("RemoveKey:" +map.remove ("the");System.out.println ("Get:" +map.get ("02")); //map.put (null, "haha");Map.put ("04",NULL); System.out.println ("Get:" +map.get ("04")); //The return value of the GE method can be used to determine whether a key exists. To judge by Null return//get all the values in the Map collectioncollection<string> coll =map.values ();        System.out.println (coll);    SYSTEM.OUT.PRINTLN (map); }}
Mapdemo

Two ways to remove a map collection:
1. set<k> KeySet: Deposit all the keys in the map into the set set because the set has an iterator,
Therefore, you can use an iterative method to remove all the keys, and then according to the Get method, get each key corresponding value.

The extraction principle of the Map collection: The Map collection is transferred to the set set and then removed by the iterator.

2. Set<map.entry<k,v>> EntrySet: The mapping relationship in the map collection is stored in the set set,
And the data type of the relationship is: map.entry

 Public classMapdemo2quzhi { Public Static voidMain (string[] args) {Map<String,String> map =NewHashmap<string,string>(); Map.put ("Zhangsan2", "the"); Map.put ("Zhangsan3", "the"); Map.put ("Zhangsan1", "the"); Map.put ("", "Zhangsan4"); //The map in the map collection is taken out and deposited into the set set. set<map.entry<string, string>> entryset =Map.entryset (); Iterator<map.entry<string, string>> it =Entryset.iterator ();  while(It.hasnext ()) {Map.entry<string, string> me =It.next (); String Key=Me.getkey (); String value=Me.getvalue (); SYSTEM.OUT.PRINTLN (Key+":"+value); }                /*first gets the set set of all keys for the map collection, KeySet ();                set<string> KeySet = Map.keyset ();                With the set set, you can get the iterator iterator<string> it = Keyset.iterator ();            while (It.hasnext ()) {String key = It.next ();            A key can get its corresponding value through the Get method of the map collection.            String value = Map.get (key);        System.out.println ("Key:" +key+ ", Value:" +value); }        */    }}/*Map.entry actually Entry is also an interface, it is a map interface in an internal interface has a map Entry, so define the inner class interface map{public static interface Entry        {public abstract Object GetKey ();    Public abstract Object GetValue (); }} class HashMap implements map{class Haha implements map.entry {public Object GetKey () {} public OBJ ECT GetValue () {}}}*/
Mapdemo2quzhi

Mao expands knowledge. One-to-many relationship!

Importjava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.Iterator;Importjava.util.List;/*Mao expands knowledge. One-to-many relationship! The map collection is used because it has a mapping relationship. "Yureban" Student ("Zhangsan"); Yureban "Student" ("Lisi"); " Jiuyeban "Student" ("Wangwu"); Jiuyeban "Student" ("Zhaoliu"); a school has multiple classrooms and each classroom has a name. */classstudent1{PrivateString ID; PrivateString name; Student1 (String id,string name) { This. ID =ID;  This. Name =name; }     PublicString toString () {returnId+ "::" +name; }} Public classMapdemo3_kuozhan { Public Static voidDemo () {HashMap<String,List<Student1>> CZBK =NewHashmap<string,list<student1>>(); List<Student1> yure =NewArraylist<student1>(); List<Student1> Jiuye =NewArraylist<student1>(); Yure.add (NewStudent1 ("Zhagnsan", "the")); Yure.add (NewStudent1 ("Wangwu", "the")); Yure.add (NewStudent1 ("Zhouqi", "the")); Yure.add (NewStudent1 ("The Zhaoliu", "the")); Czbk.put ("Yureban", yure); Czbk.put ("Jiuyeban", Jiuye); Iterator<String> it =Czbk.keyset (). iterator ();  while(It.hasnext ()) {String Roomname=It.next (); List<Student1> =Czbk.get (roomname); //System.out.println (roomname);Getinfos (guest); }    }     Public Static voidGetinfos (list<student1>list) {Iterator<Student1> it =List.iterator ();  while(It.hasnext ()) {Student1 s=It.next ();        System.out.println (s); }    }     Public Static voidMain (string[] args) {demo (); //classrooms        /* hashmap<string,hashmap<string,string>> CZBK = new Hashmap<string,hash                Map<string,string>> ();                hashmap<string,string> yure = new hashmap<string,string> ();                hashmap<string,string> Jiuye = new hashmap<string,string> ();        Czbk.put ("Yureban", yure);                Czbk.put ("Jiuyeban", Jiuye);        Yure.put ("Zhangsan");                Yure.put ("Lisi");        Jiuye.put ("Wangwu");                Jiuye.put ("Zhaoliu");                Take out all the students in the school, traverse the CZBK collection, get all the classrooms iterator<string> it = Czbk.keyset (). Iterator ();            while (It.hasnext ()) {String roomname = It.next ();                        hashmap<string,string> = Czbk.get (roomname);            System.out.println (Roomname);        Getstudentof (guest);        }//Remove all students from a class//getstudentof (Jiuye); */    }     Public Static voidGetstudentof (hashmap<string,string>Roommap) {Iterator<String> it =Roommap.keyset (). iterator ();  while(It.hasnext ()) {String ID=It.next (); String name=roommap.get (ID); System.out.println (ID+":"+name); }    }}
Mapdemo3_kuozhan
ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Set;ImportJava.util.TreeMap;/*Exercise: "SDFGZXCVSDFXCDF" gets the number of occurrences of the letter in the string. You want to print the result: a[1]c[2] ... The results show that each letter has a corresponding number of times. Indicates that there is a mapping between the letters and the number of times, note that when a mapping relationship is found, you can select the Map collection. Because the map collection stores the mapping relationship. When do you use the map collection? When there is a mapping between the data, you should think of the map collection first. Idea: 1. Converts a string into a character array, because each letter is to be manipulated. 2. Define a map collection, because the letters of the printed results are in order, so use the TreeMap collection.    3. Iterate through the character array.    Use each letter as a key to check the map collection, and if the return is null, the letter and 1 are stored in the map collection.    If the return is not NULL, it indicates that the letter already exists in the map collection and has a corresponding number of times. Then it gets the number and self-increment, and then deposits the letter and the number of times since the increment into the map collection, overwriting the value that corresponds to the original key being called.      4. Returns the data in the map collection into a specified string. */ Public classMaptest3zimu { Public Static voidMain (string[] args) {String s= CharCount ("Aabfcdabc+d-efa");    System.out.println (s); }     Public Staticstring charcount (String str) {Char[] CHS =Str.tochararray (); TreeMap<Character,Integer> TM =NewTreemap<character,integer>(); intCount = 0;  for(intx=0;x<chs.length;x++)        {            if(! (chs[x]>= ' A ' &&chs[x]<= ' Z ' | | chs[x]>= ' A ' && chs[x]<= ' Z '))                Continue; Integer value=Tm.get (chs[x]); if(value!=NULL) Count=value; Count++;            Tm.put (Chs[x], count); Count= 0; /*method Two if (Value==null) {tm.put (chs[x], 1);                } else {value = value +1;            Tm.put (Chs[x], value); }            */        }        //System.out.println (tm); //buffers want to print results: A (4) B (2) C (2) d (2) E (1) F (2) ...StringBuilder SB =NewStringBuilder (); Set<map.entry<character, integer>> entryset =Tm.entryset (); Iterator<map.entry<character, integer>> it =Entryset.iterator ();  while(It.hasnext ()) {Map.entry<character, integer> me =It.next (); Character CH=Me.getkey (); Integer value=Me.getvalue (); Sb.append (Ch+ "(" +value+ ")"); }        returnsb.tostring (); }}
Maptest3zimu

Collection single-row collection map double-column 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.