Share via


Hashtable Class

Definition

This class implements a hash table, which maps keys to values.

[Android.Runtime.Register("java/util/Hashtable", DoNotGenerateAcw=true)]
[Java.Interop.JavaTypeParameters(new System.String[] { "K", "V" })]
public class Hashtable : Java.Util.Dictionary, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ISerializable, Java.Lang.ICloneable, Java.Util.IMap
[<Android.Runtime.Register("java/util/Hashtable", DoNotGenerateAcw=true)>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "K", "V" })>]
type Hashtable = class
    inherit Dictionary
    interface ISerializable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface ICloneable
    interface IMap
Inheritance
Attributes
Implements

Remarks

This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value.

To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.

An instance of Hashtable has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. Note that the hash table is open: in the case of a "hash collision", a single bucket stores multiple entries, which must be searched sequentially. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. The initial capacity and load factor parameters are merely hints to the implementation. The exact details as to when and whether the rehash method is invoked are implementation-dependent.

Generally, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the time cost to look up an entry (which is reflected in most Hashtable operations, including get and put).

The initial capacity controls a tradeoff between wasted space and the need for rehash operations, which are time-consuming. No rehash operations will ever occur if the initial capacity is greater than the maximum number of entries the Hashtable will contain divided by its load factor. However, setting the initial capacity too high can waste space.

If many entries are to be made into a Hashtable, creating it with a sufficiently large capacity may allow the entries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.

This example creates a hashtable of numbers. It uses the names of the numbers as keys:

{@code
              Hashtable<String, Integer> numbers
                = new Hashtable<String, Integer>();
              numbers.put("one", 1);
              numbers.put("two", 2);
              numbers.put("three", 3);}

To retrieve a number, use the following code:

{@code
              Integer n = numbers.get("two");
              if (n != null) {
                System.out.println("two = " + n);
              }}

The iterators returned by the iterator method of the collections returned by all of this class's "collection view methods" are <em>fail-fast</em>: if the Hashtable is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. The Enumerations returned by Hashtable's #keys keys and #elements elements methods are <em>not</em> fail-fast; if the Hashtable is structurally modified at any time after the enumeration is created then the results of enumerating are undefined.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the

Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use java.util.concurrent.ConcurrentHashMap in place of Hashtable.

Added in 1.0.

Java documentation for java.util.Hashtable.

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.

Constructors

Hashtable()

Constructs a new, empty hashtable with a default initial capacity (11) and load factor (0.

Hashtable(IDictionary)

Constructs a new hashtable with the same mappings as the given Map.

Hashtable(Int32, Single)

Constructs a new, empty hashtable with the specified initial capacity and the specified load factor.

Hashtable(Int32)

Constructs a new, empty hashtable with the specified initial capacity and default load factor (0.

Hashtable(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsEmpty

Tests if this hashtable maps no keys to values.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

Clear()

Clears this hashtable so that it contains no keys.

Clone()

Creates a shallow copy of this hashtable.

Compute(Object, IBiFunction)

To be added

ComputeIfAbsent(Object, IFunction)

To be added

ComputeIfPresent(Object, IBiFunction)

To be added

Contains(Object)

Tests if some key maps into the specified value in this hashtable.

ContainsKey(Object)

Tests if the specified object is a key in this hashtable.

ContainsValue(Object)

Returns true if this hashtable maps one or more keys to this value.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Elements()

Returns an enumeration of the values in this hashtable.

EntrySet()

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

Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
ForEach(IBiConsumer)
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 a hash code value for the object.

(Inherited from Object)
GetOrDefault(Object, Object)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Keys()

Returns an enumeration of the keys in this hashtable.

KeySet()

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

Merge(Object, Object, IBiFunction)

To be added

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Put(Object, Object)

Maps the specified key to the specified value in this hashtable.

PutAll(IDictionary)

Copies all of the mappings from the specified map to this hashtable.

PutIfAbsent(Object, Object)
Rehash()

Increases the capacity of and internally reorganizes this hashtable, in order to accommodate and access its entries more efficiently.

Remove(Object, Object)

Removes the key (and its corresponding value) from this hashtable.

Remove(Object)

Removes the key (and its corresponding value) from this hashtable.

Replace(Object, Object, Object)
Replace(Object, Object)
ReplaceAll(IBiFunction)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
Size()

Returns the number of keys in this hashtable.

ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Values()

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

Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to