Java Core API--8 (Map collection)

Source: Internet
Author: User
Tags set set

1. Map Collection

Java.util.Map interface

|--hashmap Implementation Class

|--treemap Implementation Class


2. HASHMAP Implementation Class

A collection of map interface definitions, also known as a lookup table, is used to store so-called "key-value" key-value pairs. Key can be viewed as the index of value. And often key is part of value.

1) key cannot be duplicated, but the saved value can be repeated.

2) According to the internal structure of different, map interface has a variety of implementation classes, which are commonly used in the internal hash table for the implementation of the HashMap and the internal treemap for the sequencing binary tree implementation. It is also not recommended to store more than two data types when storing data, so it is common for us to use a generic constraint to store the type of content when using map.

3) When creating a map, use generics, there are two types to constrain, one is the type of key, and the other is the type of value.

4) Basic principle diagram:

650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M02/6F/1C/ Wkiol1wsp5hhdqcbaaclrow-f-y469.jpg "alt=" Wkiol1wsp5hhdqcbaaclrow-f-y469.jpg "/>

5) Common methods in HashMap collection:

①v put (K key,v value): Puts the element in the form of a key-value in the map. If you repeatedly save the same key, the actual operation is to replace the value of the key.

②v get (Object key): Returns the value corresponding to key. Returns null if it does not exist.

③boolean ContainsKey (Object key): Determines whether the specified Key is contained in the collection.

④boolean Containsvalue (Object value): Determines whether the collection contains the specified value.

6) If the given key does not exist in the map, it returns NULL, so, in principle, when fetching an element from a map, it is necessary to determine if there is an element, and then use it to avoid the occurrence of a null pointer exception. Map is very specific when acquiring elements, the collection wants to get elements to traverse the collection, and map does not need, you just give him a specific key to get the element.

Case 12:

650) this.width=650; "style=" WIDTH:663PX;HEIGHT:437PX; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M01/6f/1c/wkiol1wsp7wr9z0qaajvhbctqde347.jpg "width=" 694 "height=" 462 "alt=" wkiol1wsp7wr9z0qaajvhbctqde347.jpg "/ >

Case 13:

650) this.width=650; "title=" 1 clipboard.png "src=" http://s3.51cto.com/wyfs02/M01/6F/1C/ Wkiol1wsp96yorquaahrdfgfa1m333.jpg "alt=" Wkiol1wsp96yorquaahrdfgfa1m333.jpg "/>

Results:

650) this.width=650; "title=" 2 Clipboard.png "src=" http://s3.51cto.com/wyfs02/M00/6F/1F/ Wkiom1wspirbs9wuaaazfg6b89a863.jpg "alt=" Wkiom1wspirbs9wuaaazfg6b89a863.jpg "/>

7) Traverse HashMap mode one: Gets all keys and obtains value from key to achieve the traversal effect (that is, iterate key). KeySet () Method: Is the method that HashMap obtains all keys, which can get all the keys stored under the map and return as a set.

Case 14:

650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M01/6F/1C/ Wkiol1wsqazbjmalaanz-gxfoo0009.jpg "width=" 639 "height=" 519 "alt=" wkiol1wsqazbjmalaanz-gxfoo0009.jpg "/>

Results:

650) this.width=650; "style=" width:641px;height:50px; "title=" 2 Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M02/6f/1c/wkiol1wsqcggszcgaac4sbjixl8564.jpg "width=" 642 "height=" (alt= "wkiol1wsqcggszcgaac4sbjixl8564.jpg"/ >

8) Entry class, traverse HashMap mode two: Iterate in the form of "key-value pair". Map supports another method EntrySet (): The method returns a set set of elements that are each set of key-value pairs in the map, and map describes each key-value pair as an instance of the entry class. There are two methods: GetKey () gets the key value; GetValue () Gets the value. Entry also requires a generic constraint, and its constrained generics should be the same as the map! Entry Location: Java.util.Map.Entry.

Case 15:

650) this.width=650; "style=" width:656px;height:515px; "title=" 1 clipboard.png "src=" http://s3.51cto.com/wyfs02/ M02/6f/1f/wkiom1wspoicasitaaougdqas6w163.jpg "width=" 641 "height=" 510 "alt=" wkiom1wspoicasitaaougdqas6w163.jpg "/ >

Results:

650) this.width=650; "style=" width:655px;height:54px; "title=" 2 Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M00/6f/1f/wkiom1wspp3igtsbaac7w1buqde969.jpg "width=" 644 "height=" "alt=" wkiom1wspp3igtsbaac7w1buqde969.jpg "/ >

This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1669399

Java Core API--8 (Map collection)

Related Article

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.