Detailed usage of map in Java _java

Source: Internet
Author: User

Map Introduction

The object that maps the key to a value. A map cannot contain duplicate keys, and each key can be mapped to at most one value. This interface replaces the Dictionary class, which is completely an abstract class, not an interface.

The map interface provides three collection views that allow you to view the contents of a map in the form of a keyset, a set of values, or a key-value mapping relationship. The mapping order is defined as the order in which the iterator returns its elements on the mapped collection view. Some mapping implementations can explicitly guarantee their order, such as the TreeMap class, and other mapping implementations do not guarantee the order, such as the HashMap class.

Note: You must be cautious when using a mutable object as a mapping key. When an object is a key in a map, the mapping behavior is indeterminate if the value of the object is changed in a way that affects the equals comparison. A special case of this prohibition is that a mapping is not allowed to contain itself as a key. Although you allow a map to contain itself as a value, be careful: the definition of the Equals and Hashcode methods is no longer explicit on such a mapping.

All common mapping implementation classes should provide two "standard" constructs: a void (parameterless) Construction method to create an empty map, and a constructor with a single map type parameter to create a new mapping with the same key-value mapping relationship as its parameters. In fact, the latter constructor allows the user to copy any mapping and generate an equivalent mapping of the desired class. Although this recommendation cannot be enforced (because the interface cannot contain a construction method), all common mapping implementations in the JDK follow it.

The "Destroy" method contained in this interface modifies the mapping of its operations, and these methods throw unsupportedoperationexception if this mapping does not support the operation. If so, these methods can (but do not require) throw unsupportedoperationexception when the call is not valid for the mapping. For example, if an unmodified mapping (whose mapping relationship is "overlapping") is empty, you can (but not require) throw an exception when you call the Putall (map) method on the map.

Some mapping implementations limit the number of keys and values that may be contained. For example, some implementations prohibit null keys and values, while others have restrictions on the type of their keys. Attempting to insert an unqualified key or value throws an unchecked exception, usually NullPointerException or classcastexception. An attempt was made to query for the existence of unqualified keys or values that might throw an exception or false; Some implementations would show the previous behavior, while others would behave the latter. In general, an exception may be thrown, or the operation succeeds, depending on the implementation itself, when an attempt is made to perform an operation on an unqualified key or value and the completion of the operation does not cause the unqualified element to be inserted into the map. Such exceptions are marked as "optional" in the specification of this interface.

This interface is a member of the Java collections Framework.

Many of the methods in the collections Framework interface are defined according to the Equals method. For example, the specification of the ContainsKey (Object key) method reads: "Returns True if and only if this mapping contains a mapping relationship for key k that satisfies (Key==null. K==null:key.equals (k))." This specification should not be interpreted as: invoking a map.containskey with a non-null parameter key causes key.equals (k) to be invoked on any key K. Implementations are optionally optimized to avoid calling equals, for example, a hash code that compares two keys first (the Object.hashcode () specification guarantees that two objects with a hash code are not equal). In general, the implementations of various collections Framework interfaces are free to take advantage of the specified behavior of the underlying Object method as long as the implementing person deems it appropriate.

Common Operation Instructions
void Clear ()
Removes all mapping relationships (optional actions) from this map.
Boolean ContainsKey (Object key)
Returns TRUE if this map contains a mapping relationship for the specified key.
Boolean Containsvalue (Object value)
Returns true if the mapping maps one or more keys to a specified value.
Set<map.entry<k,v>> EntrySet ()
Returns the 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 null if the mapping does not contain a mapping relationship for the key.
int Hashcode ()
Returns the hash code value for this map.
Boolean IsEmpty ()
Returns true if the mapping does not contain a key-value mapping relationship.
Set<k> keyset ()
Returns the 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 action).
void Putall (map< extends K,? extends v> m)
Copies all mapping relationships from the specified mappings into this map (optional operation).
V Remove (Object key)
If a mapping relationship exists for a key, it is removed from this map (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:

Copy Code code as follows:

Map map = new HashMap ();

2. Place value in map, note: Map is stored in the form of key-value, such as:
Copy Code code as follows:

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

3. Take value from map:
Copy Code code as follows:

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

The result: str = "DD"
4. Traverse a map to get key and value from:
Copy Code code as follows:

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.