1418 views|1 replies

36

Posts

0

Resources
The OP
 

Java Development: Map Introduction [Copy link]

An object that maps keys to values. A map cannot contain duplicate keys; each key can be mapped to at most one value. This interface replaces the Dictionary class, which was entirely an abstract class, not an interface.

The Map interface provides three collection views that allow viewing the contents of a map as a set of keys, a set of values, or a set of key-value mappings. The order of a map is defined as the order in which iterators on the map's collection views return its elements. Some map implementations explicitly guarantee their order, such as the TreeMap class; others do not, such as the HashMap class.

Note: Be careful when using mutable objects as map keys. While an object is a key in a map, the behavior of the map is undefined if the object's value is changed in a way that affects equals comparisons. A special case of this prohibition is that a map is not allowed to contain itself as a key. While a map is allowed to contain itself as a value, use caution: the equals and hashCode methods on such a map are no longer well-defined.

Java development, Map introduction

All generic map implementation classes should provide two "standard" constructors: a void (no-argument) constructor that creates an empty map; and a constructor that takes a single Map type argument that creates a new map with the same key-value mapping as its argument. In effect, the latter constructor allows the user to copy an arbitrary map, generating an equivalent map of the desired class. Although this recommendation cannot be enforced (because interfaces cannot contain constructors), all generic map implementations in the JDK follow it.

The "destroy" methods contained in this interface modify the map on which they operate, and will throw UnsupportedOperationException if the map does not support the operation. If so, these methods may (but are not required to) throw UnsupportedOperationException if the call is invalid for the map. For example, if an unmodifiable map (whose mappings are "overlapping") is empty, then calling the putAll(Map) method on the map may (but is not required to) throw an exception.

Some map implementations place restrictions on the keys and values that may be contained. For example, some implementations prohibit null keys and values, while others place restrictions on the types of their keys. An attempt to insert an ineligible key or value will throw an unchecked exception, typically a NullPointerException or ClassCastException. An attempt to query the existence of an ineligible key or value may throw an exception, or return false; some implementations will exhibit the former behavior and others the latter. In general, an attempt to perform an operation on an ineligible key or value that can be completed without causing an ineligible element to be inserted into the map may either throw an exception or succeed, depending on the implementation. Such exceptions are marked as "optional" in the specification of this interface.

This interface is a member of the Java Collections Framework.

Many methods in the Collections Framework interfaces are defined in terms of the equals method. For example, the specification for the containsKey(Object key) method reads, "Returns true if and only if this map contains a mapping for key k such that (key==null ? k==null : key.equals(k))". This specification should not be interpreted to mean that calling Map.containsKey with a non-null argument key will cause key.equals(k) to be called for any key k. Implementations are free to make optimizations to avoid calling equals, for example by comparing the hash codes of the two keys first (the Object.hashCode() specification guarantees that two objects with unequal hash codes will not be equal). In general, implementations of the various Collections Framework interfaces are free to take advantage of the specified behavior of the underlying Object methods as the implementor sees fit.

Common operating instructions

void clear()

Removes all mappings from this map (optional operation).

boolean containsKey(Object key)

Returns true if this map contains a mapping for the specified key.

boolean containsValue(Object value)

Returns true if this map maps one or more keys to the specified value.

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

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

boolean equals(Object o)

Compares the specified object with this map for equality.

V get(Object key)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

int hashCode()

Returns a hash code value for this map.

boolean isEmpty()

Returns true if this map contains no key-value mappings.

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 a mapping exists for the key, removes it from this map (optional operation).

int size()

Returns the number of key-value mappings 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. Put values into the map. Note: the map is stored in the form of key-value, such as:

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

3. Get values from the map:

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

The result is: str = "dd'

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

Map m = new HashMap();

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

Object value = map.get(obj);

}

This post is from Talking

Latest reply

Map is a very useful structure. A lot of caches in the web are actually maps.   Details Published on 2021-4-12 22:58
 
 

7452

Posts

2

Resources
2
 

Map is a very useful structure. A lot of caches in the web are actually maps.

This post is from Talking
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list