Reproduced in Java map of the detailed

Source: Internet
Author: User

About Map

What is a map?

A map is an interface that is an object that maps a key to a value.

What is the main role of map?

A description of the data values and values can also be obtained by creating a map's implementation class to store the data

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 supersedes the Dictionary class, which is entirely 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, value set, or key-value mapping relationship set. The mapping order is defined as the order in which iterators return their elements on the mapped collection view. Some mapping implementations can explicitly guarantee their order, such as the TreeMap class, while others do not guarantee the order, such as the HashMap class.

Note: You must be extra careful when using mutable objects as mapping keys. When an object is a key in a map, if the value of the object is changed in a way that affects the equals comparison, the mapping behavior will be indeterminate. A special case of this prohibition is that a map is not allowed to contain itself as a key. While allowing a map to include itself as a value, take extra care: the definition of the Equals and hashcode methods on such mappings will no longer be clear.

All common mapping implementation classes should provide two "standard" construction Methods: a void (parameterless) construction method for creating an empty map, and a construction method with a single map type parameter that creates a new mapping with the same key-value mappings as its arguments. In fact, the latter constructor allows the user to copy arbitrary mappings, generating an equivalent mapping of the required classes. Although this recommendation cannot be enforced (because an interface cannot contain a constructor method), all common mapping implementations in the JDK follow it.

The "Destroy" method contained in this interface modifies the mapping of its operation, which throws unsupportedoperationexception if this mapping does not support the operation. If so, these methods can (but are not required) throw unsupportedoperationexception when the call to the mappings is invalid. For example, if a non-modifiable mapping (whose mapping is "overlapping") is empty, an exception can be thrown (but not required) when the Putall (map) method is called on the map.

Some mapping implementations have restrictions on the keys and values that may be included. For example, some implementations prohibit null keys and values, 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. Attempts to query for the existence of unqualified keys or values may throw an exception, or return false; Some implementations will exhibit the previous behavior, while others behave the latter. In general, an attempt to perform an operation on an unqualified key or value and the completion of the operation does not result in an unqualified element being inserted into the map, an exception may be thrown, or the operation succeeds, depending on the implementation itself. 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: calling Map.containskey with a non-null parameter key will result in the invocation of Key.equals (k) on any key K. Implementations can be arbitrarily optimized to avoid calling equals, for example, a hash code of two keys can be compared first (the Object.hashcode () specification guarantees that two objects with unequal hash codes are not equal). In general, implementations of various collections Framework interfaces are free to take advantage of the specified behavior of the underlying Object method as long as the implementation deems fit.

Common Operating Instructions

void Clear ()

Removes all mapping relationships from this mapping (optional).

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 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 null if 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 (); Polymorphic use of Parent-class references to child-class objects

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"

4. Traverse a map to get the key and value from:

Map m= new HashMap ();

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

Object value = map.get (obj);

}

Use of Map

/**
* Create MAP constraint data type
*/
Map<string, integer> map=new hashmap<string,integer> ();

/*
* Put method to store data in map
*/
Integer i=map.put ("math", "n");
System.out.println (i);
Map.put ("language", 96);
Map.put ("English", 80);
Map.put ("Language 2", 96);
Map.put ("English 2", 80);
Map.put ("Language 1", 96);
Map.put ("English 1", 80);
int Ii=map.put ("mathematics", 80);
System.out.println (ii);
System.out.println (map);
/**
* Get elements from map
* V get (key)
*/
Integer mun=map.get ("language");
System.out.println (MUN);
/**
* If key does not exist, returns NULL
* cannot be accepted at this time by int type
* NULL cannot invoke Intvalue () method to implement auto-unpacking
* Report NULL pointer exception
*/
//int ii2= Map.get ("high number");
//system.out.println (ii);
/**
* To determine if the map contains a key
*/
if (Map.containskey ("math")) {
System.out.println ("contains");
} else System.out.println ("not included");
/**
* Delete an element
*/
System.out.println (map);
Integer Ii2=map.remove ("Language 1");
System.out.println ("Delete the value of the element:" +ii2);
System.out.println (map);


/** * Three ways to traverse a map *

1. Variable all key
*
* 2. Convenient key-value pairs for a row
*
* 3. Facilitate all the value
*/
public class Traverse Map {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Map<string, integer> map=new hashmap<string,integer> ();

Map.put ("Language", 96);
Map.put ("English", 80);
Map.put ("Language 2", 96);
Map.put ("English 2", 80);
Map.put ("Language 1", 96);
Map.put ("English 1", 80);
/**
* 1 set<k> KeySet ()
*/
set<string > Keyset=map.keyset ();
for (String Str:keyset) {
System.out.println (str);
Str= "GG";
}
for (String Str:keyset) {
System.out.println (str);
}

/**
* 2. Set<entry> EntrySet ();
* Traversing key-value pairs
*/
Set<entry<string, integer>> EntrySet
=map.entryset ();
for (entry<string,integer> E:entryset) {
System.out.println (E.getkey () + ":" +e.getvalue ());

}
/**
* 3. Traverse all the value
*/
Collection<integer> cc=map.values ();
for (Integer ii:cc) {
SYSTEM.OUT.PRINTLN (ii);
}
}

Reproduced in Java map of the detailed

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.