Compartilhar via


IMap Interface

Definition

An object that maps keys to values.

[Android.Runtime.Register("java/util/Map", "", "Java.Util.IMapInvoker")]
[Java.Interop.JavaTypeParameters(new System.String[] { "K", "V" })]
public interface IMap : Android.Runtime.IJavaObject, IDisposable, Java.Interop.IJavaPeerable
[<Android.Runtime.Register("java/util/Map", "", "Java.Util.IMapInvoker")>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "K", "V" })>]
type IMap = interface
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Derived
Attributes
Implements

Remarks

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface.

The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map.

All general-purpose map implementation classes should provide two "standard" constructors: a void (no arguments) constructor which creates an empty map, and a constructor with a single argument of type Map, which creates a new map with the same key-value mappings as its argument. In effect, the latter constructor allows the user to copy any map, producing an equivalent map of the desired class. There is no way to enforce this recommendation (as interfaces cannot contain constructors) but all of the general-purpose map implementations in the JDK comply.

The "destructive" methods contained in this interface, that is, the methods that modify the map on which they operate, are specified to throw UnsupportedOperationException if this map does not support the operation. If this is the case, these methods may, but are not required to, throw an UnsupportedOperationException if the invocation would have no effect on the map. For example, invoking the #putAll(Map) method on an unmodifiable map may, but is not required to, throw the exception if the map whose mappings are to be "superimposed" is empty.

Some map implementations have restrictions on the keys and values they may contain. For example, some implementations prohibit null keys and values, and some have restrictions on the types of their keys. Attempting to insert an ineligible key or value throws an unchecked exception, typically NullPointerException or ClassCastException. Attempting to query the presence of an ineligible key or value may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible key or value whose completion would not result in the insertion of an ineligible element into the map may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as "optional" in the specification for this interface.

Many methods in Collections Framework interfaces are defined in terms of the Object#equals(Object) equals method. For example, the specification for the #containsKey(Object) containsKey(Object key) method says: "returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k))." This specification should not be construed to imply that invoking Map.containsKey with a non-null argument key will cause key.equals(k) to be invoked for any key k. Implementations are free to implement optimizations whereby the equals invocation is avoided, for example, by first comparing the hash codes of the two keys. (The Object#hashCode() specification guarantees that two objects with unequal hash codes cannot be equal.) More generally, implementations of the various Collections Framework interfaces are free to take advantage of the specified behavior of underlying Object methods wherever the implementor deems it appropriate.

Some map operations which perform recursive traversal of the map may fail with an exception for self-referential instances where the map directly or indirectly contains itself. This includes the clone(), equals(), hashCode() and toString() methods. Implementations may optionally handle the self-referential scenario, however most current implementations do not do so.

<h2>"unmodifiable">Unmodifiable Maps</h2>

The Map#of() Map.of, Map#ofEntries(Map.Entry...) Map.ofEntries, and Map#copyOf Map.copyOf static factory methods provide a convenient way to create unmodifiable maps. The Map instances created by these methods have the following characteristics:

<ul> <li>They are <i>unmodifiable</i>. Keys and values cannot be added, removed, or updated. Calling any mutator method on the Map will always cause UnsupportedOperationException to be thrown. However, if the contained keys or values are themselves mutable, this may cause the Map to behave inconsistently or its contents to appear to change. <li>They disallow null keys and values. Attempts to create them with null keys or values result in NullPointerException. <li>They are serializable if all keys and values are serializable. <li>They reject duplicate keys at creation time. Duplicate keys passed to a static factory method result in IllegalArgumentException. <li>The iteration order of mappings is unspecified and is subject to change. <li>They are value-based. Programmers should treat instances that are #equals(Object) equal as interchangeable and should not use them for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. Callers should make no assumptions about the identity of the returned instances. Factories are free to create new instances or reuse existing ones. <li>They are serialized as specified on the Serialized Form page. </ul>

This interface is a member of the Java Collections Framework.

Added in 1.2.

Java documentation for java.util.Map.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Properties

Handle

Gets the JNI value of the underlying Android object.

(Inherited from IJavaObject)
IsEmpty

Returns whether this map is empty.

JniIdentityHashCode

Returns the value of java.lang.System.identityHashCode() for the wrapped instance.

(Inherited from IJavaPeerable)
JniManagedPeerState

State of the managed peer.

(Inherited from IJavaPeerable)
JniPeerMembers

Member access and invocation support.

(Inherited from IJavaPeerable)
PeerReference

Returns a JniObjectReference of the wrapped Java object instance.

(Inherited from IJavaPeerable)

Methods

Clear()

Removes all of the mappings from this map (optional operation).

Compute(Object, IBiFunction)

Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).

ComputeIfAbsent(Object, IFunction)

If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.

ComputeIfPresent(Object, IBiFunction)

If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.

ContainsKey(Object)

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

ContainsValue(Object)

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

CopyOf(IDictionary)

Returns an unmodifiable Map containing the entries of the given Map.

Disposed()

Called when the instance has been disposed.

(Inherited from IJavaPeerable)
DisposeUnlessReferenced()

If there are no outstanding references to this instance, then calls Dispose(); otherwise, does nothing.

(Inherited from IJavaPeerable)
Entry(Object, Object)

Returns an immutable Entry containing the given key and value.

EntrySet()

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

Equals(Object)

Compares the specified object with this map for equality.

Finalized()

Called when the instance has been finalized.

(Inherited from IJavaPeerable)
ForEach(IBiConsumer)

Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.

Get(Object)

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

GetHashCode()

Returns the hash code value for this map.

GetOrDefault(Object, Object)

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

KeySet()

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

Merge(Object, Object, IBiFunction)

If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.

Of()

Returns an unmodifiable map containing zero mappings.

Of(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object)

Returns an unmodifiable map containing ten mappings.

Of(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object)

Returns an unmodifiable map containing nine mappings.

Of(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object)

Returns an unmodifiable map containing eight mappings.

Of(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object)

Returns an unmodifiable map containing seven mappings.

Of(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object)

Returns an unmodifiable map containing six mappings.

Of(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object)

Returns an unmodifiable map containing five mappings.

Of(Object, Object, Object, Object, Object, Object, Object, Object)

Returns an unmodifiable map containing four mappings.

Of(Object, Object, Object, Object, Object, Object)

Returns an unmodifiable map containing three mappings.

Of(Object, Object, Object, Object)

Returns an unmodifiable map containing two mappings.

Of(Object, Object)

Returns an unmodifiable map containing a single mapping.

OfEntries(IMapEntry[])

Returns an immutable map containing keys and values extracted from the given entries.

Put(Object, Object)

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

PutAll(IDictionary)

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

PutIfAbsent(Object, Object)

If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.

Remove(Object, Object)

Removes the entry for the specified key only if it is currently mapped to the specified value.

Remove(Object)

Removes the mapping for a key from this map if it is present (optional operation).

Replace(Object, Object, Object)

Replaces the entry for the specified key only if currently mapped to the specified value.

Replace(Object, Object)

Replaces the entry for the specified key only if it is currently mapped to some value.

ReplaceAll(IBiFunction)

Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.

SetJniIdentityHashCode(Int32)

Set the value returned by JniIdentityHashCode.

(Inherited from IJavaPeerable)
SetJniManagedPeerState(JniManagedPeerStates) (Inherited from IJavaPeerable)
SetPeerReference(JniObjectReference)

Set the value returned by PeerReference.

(Inherited from IJavaPeerable)
Size()

Returns the number of key-value mappings in this map.

UnregisterFromRuntime()

Unregister this instance so that the runtime will not return it from future Java.Interop.JniRuntime+JniValueManager.PeekValue invocations.

(Inherited from IJavaPeerable)
Values()

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

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to