Map Study Notes

Source: Internet
Author: User

Collection is a single-column collection, and map is a double-row collection. It contains the <k,v> key-value pairs, note that the keys are unique and the values are not unique.

Here are three ways to read: Keyset,valueset, and EntrySet.

Keyset is the collection that gets all the keys. Valueset is to get all worthwhile collections. EntrySet is a collection of all entries, and entry is the meaning of each entry (the key and the value).

The specific usage is:

1  PackageJavastudy;2 3 Importjava.util.Collection;4 ImportJava.util.HashMap;5 ImportJava.util.Iterator;6 ImportJava.util.Map.Entry;7 ImportJava.util.Set;8 9  Public classTextmap {Ten      Public Static voidMain (String args[]) One     { A         //Create a HashMap array (note: Because the map type is a key-value pair, and the type is worth a larger initial) -         //hashmap<float, string> hp =new hashmap<float,string> (); -hashmap<string,string> HP =NewHashmap<string,string>(); the         //use the put definition to map out the actual keys and values -Hp.put ("010", "Beijing"); -Hp.put ("021", "Shanghai"); -Hp.put ("022", "Chongqing"); +Hp.put ("023", "Tianjin"); -         //traversing prints its array +         //get a collection of all keys ASet<string> sd=Hp.keyset (); atIterator<string> zips=sd.iterator (); -           while(Zips.hasnext ()) -          { -              //get the key value -String zip=Zips.next (); -              //because the key value is unique, the value is obtained from each key, the key is unique, and the value is not unique.  inString cityname=hp.get (Zip); -System.out.println (zip+ ":" +cityname); to          } +         //values get all worthwhile collections -Collection<string> cs=hp.values (); theIterator<string> it =cs.iterator (); *           while(It.hasnext ()) $          {Panax Notoginseng System.out.println (It.next ()); -          } the  +         //EntrySet is printed to get all entries, entry= one line, meaning of an entry; ASet<entry<string, string>> se=Hp.entryset (); theIterator<entry<string, string>> ie=se.iterator (); +           while(Ie.hasnext ()) -          { $              //The key is unique, the value is not unique; Getkey is the collection of keys, GetValue gets all the worth of the collection; $Entry<string, string> es=Ie.next (); -System.out.println (Es.getkey () + ":" +Es.getvalue ()); -          } the           -     }Wuyi  the}

Also try to use a custom concept to create a sort of set of classes for the comparator and its people, specifically (if you have questions, refer to the previous chapter for a specific introduction):

1  PackageJavastudy;2 3 ImportJava.util.Comparator;4 ImportJava.util.Iterator;5 ImportJava.util.Map.Entry;6 ImportJava.util.Set;7 ImportJava.util.TreeMap;8 9  Public classTexttree {Ten  One      Public Static voidMain (string[] args) { A         //TODO auto-generated Method Stub -Treemap<people,people> tm=NewTreemap<people,people> (NewMyComp ());  -Tm.put (NewPeople ("Mark", 12),NewPeople ("Jine", 19)); theTm.put (NewPeople ("Keven", 16),NewPeople ("Bob", 20)); -Set<entry<people,people>> SE =Tm.entryset (); -Iterator<entry<people, people>> is=se.iterator (); -          while(Is.hasnext ()) +         { -Entry<people, people>es=Is.next (); +System.out.println (Es.getkey () + "\ T" +Es.getvalue ()); A         } at     } - } - classMyCompImplementsComparator<people> - { -  - @Override in      Public intCompare (People O1, people O2) { -         //TODO auto-generated Method Stub to         returnO1.name.compareTo (o2.name); +     } -      the }  * classpeople $ {Panax Notoginseng String name; -     intAge ; thePeople (String name,intAge ) +     { A          This. name=name; the          This. age=Age ; +     } -     voidShow () $     { $System.out.println (String.Format ("Name =%s, age =%d", Name,age)); -     } - @Override the      PublicString toString () { -         return"Name=" + name + ", age=" +Age ;Wuyi     } the}

Map Study Notes

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.