Initial knowledge of the Java collection--mapping table (map)

Source: Internet
Author: User

* Mapping Table
* Java Class Library provides two common implementations of HashMap and treemap for a mapping table
* HashMap hashes the keys, treemap the elements in the overall order of the keys, and constructs them into a search tree
*
* Similar to set, HashMap is faster than treemap, and does not need to be accessed by arrangement, but should also be selected HashMap
* Note that the value of the key must be unique, if a second put has the same key value in, the second value will replace the first value, put will
* Returns the previous value stored with this key parameter
*
* We can get a view of the map, which is a set of views that implement the collection interface object, or its sub-interfaces
* There are 3 views, respectively
* Key set, value set (simple set, not set), key/value pair Set. The key and the Key/value form a set, because a key in the map
* Only one copy is allowed
*
* The following three methods return the 3 views previously described
* set<k> KeySet () (keySet does not belong to HashSet and treeset, but objects that implement some other class of the Set Interface)
* Collection<v> Values ()
* set<map<key, value>> EntrySet ()
*
* You cannot add elements to the view of a keyset because it is meaningless to simply add a key.
* EntrySet also has the same limitations, but conceptually, adding new key/value pairs is meaningful

Example

1  public Static voidmain (string[] Args) {2     3map<string, employee> staff=NewHashmap<>();4Staff.put ("144-25-5464",NewEmployee ("Amy Lee"));5Staff.put ("576-24-2546",NewEmployee ("Harry hacker"));6Staff.put ("334-45-5174",NewEmployee ("Gary Cooper"));7Staff.put ("124-75-0174",NewEmployee ("francesca Cruz"));8     9 System.out.println (staff);Ten      oneStaff.remove ("576-24-2546"); a      -Staff.put ("124-75-0174",NewEmployee ("new Francesca Cruz")); -      theSystem.out.println (staff.get ("334-45-5174")); -      -     //Iterative Access -      for(map.entry<string, employee>Entry:staff.entrySet ()) { +String key =Entry.getkey (); -Employee value =Entry.getvalue (); +System.out.println ("key=" +key+ ", value" +value); a     } at}

Initial knowledge of the Java collection--mapping table (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.