Use of the Map interface

Source: Internet
Author: User

1Method Description

void Clear ()

Removes all mapping relationships from this mapping (optional).

Boolean ContainsKey (Object key)

Returns trueif this map contains a mapping relationship for the specified key.

Boolean Containsvalue (Object value)

Returns trueif the map maps one or more keys to the specified value.

Set<map.entry<k,v>> EntrySet ()

Returns a Set view of the mapping relationships contained in this map .

Boolean equals (Object O)

Compares whether the specified object is equal to this mapping.

V get (Object key)

Returns the value mapped by the specified key, or nullif the mapping does not contain a mapping relationship for the key.

int Hashcode ()

Returns the hash code value for this mapping.

Boolean IsEmpty ()

Returns true if the mapping does not contain a key - value mapping relationship .

Set<k> KeySet ()

Returns a Set view of the keys contained in this map .

V Put (K key, V value)

Associates the specified value with the specified key in this map (optional operation).

void Putall (map<? extends K,? extends v> m)

Copies all mappings from the specified map to this map (optional operation).

V Remove (Object key)

If there is a mapping of a key, it is removed from this mapping (optional operation).

int size ()

Returns the number of key - Value mapping relationships in this map.

Collection<v> VALUES ()

Returns a Collection view of the values contained in this map .

general usage of Map

1. declare a Map :

Map map = new HashMap ();

2. place values in map , Note: map is stored in key-value form, such as:

Map.put ("sa", "DD");

3. take the value from the map :

String str = map.get ("sa"). ToString,

The result is: str = "DD"

4Add, delete operations:

Object put (object key, Object value): places a keyword associated with one value into the image. If the keyword already exists, the new value associated with this keyword supersedes the old value. method returns the old value of the keyword, or null if the keyword does not originally exist

Object remove (Object key): remove key -related mappings from the image

void Putall (Map t): adds all elements from a specific image to the image

void Clear (): Remove all mappings from the image

Both the key and the value can be null. However, you cannot Add a Map as a key or value to itself. "

5Query operation:

Object get (Object key): gets The value associated with the keyword key and returns the object associated with the keyword key, if the keyword is not found in the image. Then NULL is returned

Boolean ContainsKey (Object key): Determine if there is a keyword key in the image

Boolean Containsvalue (Object value): Determines whether the value exists in the image

int size (): Returns the number of mappings in the current image

Boolean isEmpty () : Determine if there are any mappings in the image

6View actions: Working with key in image/Value pair Group

Set KeySet (): returns the set of views for all keywords in the image

"Because the collection of keys in the map must be unique, you use set support. You can also delete an element from the view, while the keyword and its associated values are removed from the source image, but you cannot add any elements. "

Collection values (): A View set that returns all values in the image

"Because the collection of values in the map is not unique, you use Collection support. You can also delete an element from the view, while the value and its keywords will be removed from the source image, but you cannot add any elements. "

Set EntrySet (): returns The view set of the map.entry object, which is the keyword / value pair in the image

"Because the mappings are unique, you use Set support." You can also remove elements from the view, and these elements will be removed from the source image, but you cannot add any elements. "

7. HashMapClasses andTreeMapclass

The "collection Framework" provides two generalMapimplementation:HashMapand theTreeMap (TreeMapImplementSortedMapInterface). In theMapto insert, delete, and position elements in theHashMapis the best choice. But if you want to traverse the key in natural order or in a custom order,TreeMapwould be better. UseHashMapthe key class required to be added clearly defines thehashcode ()and theequals ()the implementation.

There is no tuning option for this TreeMap because the tree is always in a balanced state.

7.1. HashMapclass

To optimize The use of HashMap space, you can tune the initial capacity and load factor.

(1) HashMap (): build an empty hash map image

(2) HashMap (map m): build a hash map image and add All the mappings for the images m

(3) HashMap (int initialcapacity): build an empty hash map image with a specific capacity

(4) HashMap (int initialcapacity, float loadfactor): constructs an empty hash map image with a specific capacity and load factor

7.2. TreeMapclass

TreeMap has no tuning option because the tree is always in equilibrium.

(1) TreeMap (): building an empty image tree

(2) TreeMap (Map m): Build an image tree and add all elements in image m

(3) TreeMap (Comparator C): Build an image tree and sort the keywords using a specific comparer

(4) TreeMap (SortedMap s): build an image tree, add all the mappings in the image tree s , and sort with the same comparer as the ordered image s

Use of the Map interface

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.