Detailed description of Map usage in Java and JavaMap usage

Source: Internet
Author: User

Detailed description of Map usage in Java and JavaMap usage

Introduction to Map

Objects that map keys to values. A ing cannot contain duplicate keys. Each key can only be mapped to one value at most. This interface replaces the Dictionary class, which is an abstract class rather than an interface.

The Map interface provides three collection views, allowing you to view the content of a ing in the form of a key set, value set, or key-value ing link set. The ing order is defined as the order in which the iterator returns its elements in the ing collection view. Some ing implementations can clearly ensure their order, such as the TreeMap class. Other ing implementations do not guarantee the order, such as the HashMap class.

Note: Be careful when using a mutable object as a ing key. When an object is mapped to a key, if the value of the object is changed in the way that the equals comparison is affected, the ing behavior will be uncertain. A special case of this prohibition is that a ing is not allowed to include itself as a key. Although a ing is allowed to include itself as a value package, be careful that the equals and hashCode methods are no longer clearly defined in such ing.

All common ing implementation classes should provide two "standard" constructor Methods: one void (No parameter) constructor for creating empty mappings; A constructor with a single Map type parameter is used to create a new ing with the same key-value ing relationship as its parameter. In fact, the next constructor allows users to copy arbitrary mappings and generate an equivalent ing for the required classes. Do not enforce this suggestion (because the interface cannot contain constructor methods), but all common ing implementations in JDK follow it.

The "Destroy" method contained in this interface can modify the ing of its operations. If this ing does not support this operation, these methods will throw an UnsupportedOperationException. If this is the case, the UnsupportedOperationException can be thrown by these methods (but not required) when the ing is invalid. For example, if a non-modifiable ing (whose ing relationship is "overlapped") is null, you can (but not required) Call the putAll (Map) method for the ing) throw an exception.

Some ing implementations have limits on possible keys and values. For example, some implementations disable null keys and values, while others impose restrictions on the type of their keys. Inserting an unqualified key or value will throw an unchecked exception, typically NullPointerException or ClassCastException. An attempt to check whether a key or value is unqualified may throw an exception or return false; some implementations will show the previous behavior, while others will show the latter. In general, when you try to perform an operation on an unqualified key or value and the operation is completed will not cause the unqualified element to be inserted into the ing, an exception may be thrown or the operation may be successful, this depends on the implementation. This exception is marked as "optional" in this interface specification ".

This interface is a member of Java Collections Framework.

Many methods in the Collections Framework interface are defined according to the equals method. For example, the containsKey (Object key) method canonicalized: "if and only when this ing contains? K = null: The ing between key. equals (k) and key k returns true ". This specification should not be interpreted as: Calling Map. containsKey with a non-null parameter key will result in calling key. equals (k) For any key k ). The implementation can be optimized at will to avoid calling equals. For example, you can first compare the hash codes of two keys (Object. hashCode () specifications to ensure that the two objects with different hash codes will not be equal ). Generally, as long as the implementers think it is appropriate, the implementation of various Collections Framework interfaces can freely use the specified behavior of the underlying Object method.

Common Operations

Void clear ()

Remove all mappings from this ing (optional ).

Boolean containsKey (Object key)

Returns true if the ing contains the ing relationship of the specified key.

Boolean containsValue (Object value)

Returns true if the ing maps one or more keys to a specified value.

Set <Map. Entry <K, V> entrySet ()

Returns the Set view of the ing relationships contained in the ing.

Boolean equals (Object o)

Compare whether the specified object is equal to this ing.

V get (Object key)

Returns the value mapped to the specified key. If this ing does not contain the ing relationship of the key, null is returned.

Int hashCode ()

Returns the hash value of this ing.

Boolean isEmpty ()

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

Set <K> keySet ()

Returns the Set view of the key contained in the ing.

V put (K key, V value)

Associate the specified value with the specified key in the ing (optional ).

Void putAll (Map <? Extends K ,? Extends V> m)

Copies all mappings from a specified ing to this ing (optional ).

V remove (Object key)

If a key ing relationship exists, remove it from the ing (optional ).

Int size ()

Returns the number of key-value ing relationships in the ing.

Collection <V> values ()

Returns the Collection view of the values contained in this ing.

General Map usage

1. Declare a Map:

Map map = new HashMap ();

2. Place values in map. Note: map is stored in the form of key-value, for example:

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

3. values from map:

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

Result: str = "dd'

4. traverse a map to obtain the key and value:

Map m = new HashMap ();

For (Object obj: map. keySet ()){

Object value = map. get (obj );

}

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.