Show / Hide Table of Contents

    Class HashMap<TKey, TValue>

    A C# emulation of the Java Hashmap

    A System.Collections.Generic.Dictionary`2 is a close equivalent to the Java Hashmap. One difference java implementation of the class is that the Hashmap supports both null keys and values, where the C# Dictionary only supports null values not keys. Also, V Get(TKey) method in Java returns null if the key doesn't exist, instead of throwing an exception. This implementation doesn't throw an exception when a key doesn't exist, it will return null. This class is slower than using a System.Collections.Generic.Dictionary`2, because of extra checks that have to be done on each access, to check for null.

    NOTE: This class works best with nullable types. default(T) is returned when a key doesn't exist in the collection (this being similar to how Java returns null). Therefore, if the expected behavior of the java code is to execute code based on if the key exists, when the key is an integer type, it will return 0 instead of null.

    Consider also implementing IDictionary, IEnumerable, and ICollection like System.Collections.Generic.Dictionary`2 does, so HashMap can be used in substituted in place for the same interfaces it implements.

    Inheritance
    System.Object
    HashMap<TKey, TValue>
    IdentityHashMap<TKey, TValue>
    LinkedHashMap<TKey, TValue>
    Implements
    System.Collections.Generic.IDictionary<TKey, TValue>
    System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey, TValue>>
    System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey, TValue>>
    System.Collections.IEnumerable
    Inherited Members
    System.Object.Equals(System.Object, System.Object)
    System.Object.GetType()
    System.Object.MemberwiseClone()
    System.Object.ReferenceEquals(System.Object, System.Object)
    Namespace: Lucene.Net.Support
    Assembly: Lucene.Net.dll
    Syntax
    public class HashMap<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
    Type Parameters
    Name Description
    TKey

    The type of keys in the dictionary

    TValue

    The type of values in the dictionary

    Remarks

    Unordered Dictionaries

    • System.Collections.Generic.Dictionary`2 - use when order is not important and all keys are non-null.
    • HashMap<TKey, TValue> - use when order is not important and support for a null key is required.

    Ordered Dictionaries

    • LinkedHashMap<TKey, TValue> - use when you need to preserve entry insertion order. Keys are nullable.
    • System.Collections.Generic.SortedDictionary`2 - use when you need natural sort order. Keys must be unique.
    • TreeDictionary<K, V> - use when you need natural sort order. Keys may contain duplicates.
    • LurchTable<TKey, TValue> - use when you need to sort by most recent access or most recent update. Works well for LRU caching.

    Constructors

    | Improve this Doc View Source

    HashMap()

    Declaration
    public HashMap()
    | Improve this Doc View Source

    HashMap(IEnumerable<KeyValuePair<TKey, TValue>>)

    Declaration
    public HashMap(IEnumerable<KeyValuePair<TKey, TValue>> other)
    Parameters
    Type Name Description
    System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey, TValue>> other
    | Improve this Doc View Source

    HashMap(IEqualityComparer<TKey>)

    Declaration
    public HashMap(IEqualityComparer<TKey> comparer)
    Parameters
    Type Name Description
    System.Collections.Generic.IEqualityComparer<TKey> comparer
    | Improve this Doc View Source

    HashMap(Int32)

    Declaration
    public HashMap(int initialCapacity)
    Parameters
    Type Name Description
    System.Int32 initialCapacity
    | Improve this Doc View Source

    HashMap(Int32, IEqualityComparer<TKey>)

    Declaration
    public HashMap(int initialCapacity, IEqualityComparer<TKey> comparer)
    Parameters
    Type Name Description
    System.Int32 initialCapacity
    System.Collections.Generic.IEqualityComparer<TKey> comparer

    Properties

    | Improve this Doc View Source

    Count

    Declaration
    public virtual int Count { get; }
    Property Value
    Type Description
    System.Int32
    | Improve this Doc View Source

    IsReadOnly

    Declaration
    public virtual bool IsReadOnly { get; }
    Property Value
    Type Description
    System.Boolean
    | Improve this Doc View Source

    Item[TKey]

    Declaration
    public virtual TValue this[TKey key] { get; set; }
    Parameters
    Type Name Description
    TKey key
    Property Value
    Type Description
    TValue
    | Improve this Doc View Source

    Keys

    Declaration
    public virtual ICollection<TKey> Keys { get; }
    Property Value
    Type Description
    System.Collections.Generic.ICollection<TKey>
    | Improve this Doc View Source

    Values

    Declaration
    public virtual ICollection<TValue> Values { get; }
    Property Value
    Type Description
    System.Collections.Generic.ICollection<TValue>

    Methods

    | Improve this Doc View Source

    Add(TKey, TValue)

    Declaration
    public virtual void Add(TKey key, TValue value)
    Parameters
    Type Name Description
    TKey key
    TValue value
    | Improve this Doc View Source

    Add(KeyValuePair<TKey, TValue>)

    Declaration
    public virtual void Add(KeyValuePair<TKey, TValue> item)
    Parameters
    Type Name Description
    System.Collections.Generic.KeyValuePair<TKey, TValue> item
    | Improve this Doc View Source

    AddIfAbsent(TKey, TValue)

    Declaration
    public TValue AddIfAbsent(TKey key, TValue value)
    Parameters
    Type Name Description
    TKey key
    TValue value
    Returns
    Type Description
    TValue
    | Improve this Doc View Source

    Clear()

    Declaration
    public virtual void Clear()
    | Improve this Doc View Source

    Contains(KeyValuePair<TKey, TValue>)

    Declaration
    public virtual bool Contains(KeyValuePair<TKey, TValue> item)
    Parameters
    Type Name Description
    System.Collections.Generic.KeyValuePair<TKey, TValue> item
    Returns
    Type Description
    System.Boolean
    | Improve this Doc View Source

    ContainsKey(TKey)

    Declaration
    public virtual bool ContainsKey(TKey key)
    Parameters
    Type Name Description
    TKey key
    Returns
    Type Description
    System.Boolean
    | Improve this Doc View Source

    ContainsValue(TValue)

    Declaration
    public bool ContainsValue(TValue value)
    Parameters
    Type Name Description
    TValue value
    Returns
    Type Description
    System.Boolean
    | Improve this Doc View Source

    CopyTo(KeyValuePair<TKey, TValue>[], Int32)

    Declaration
    public virtual void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
    Parameters
    Type Name Description
    System.Collections.Generic.KeyValuePair<TKey, TValue>[] array
    System.Int32 arrayIndex
    | Improve this Doc View Source

    Equals(Object)

    Declaration
    public override bool Equals(object obj)
    Parameters
    Type Name Description
    System.Object obj
    Returns
    Type Description
    System.Boolean
    Overrides
    System.Object.Equals(System.Object)
    | Improve this Doc View Source

    GetEnumerator()

    Declaration
    public virtual IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
    Returns
    Type Description
    System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<TKey, TValue>>
    | Improve this Doc View Source

    GetHashCode()

    Declaration
    public override int GetHashCode()
    Returns
    Type Description
    System.Int32
    Overrides
    System.Object.GetHashCode()
    | Improve this Doc View Source

    Remove(TKey)

    Declaration
    public virtual bool Remove(TKey key)
    Parameters
    Type Name Description
    TKey key
    Returns
    Type Description
    System.Boolean
    | Improve this Doc View Source

    Remove(KeyValuePair<TKey, TValue>)

    Declaration
    public virtual bool Remove(KeyValuePair<TKey, TValue> item)
    Parameters
    Type Name Description
    System.Collections.Generic.KeyValuePair<TKey, TValue> item
    Returns
    Type Description
    System.Boolean
    | Improve this Doc View Source

    ToString()

    Declaration
    public override string ToString()
    Returns
    Type Description
    System.String
    Overrides
    System.Object.ToString()
    | Improve this Doc View Source

    TryGetValue(TKey, out TValue)

    Declaration
    public virtual bool TryGetValue(TKey key, out TValue value)
    Parameters
    Type Name Description
    TKey key
    TValue value
    Returns
    Type Description
    System.Boolean

    Explicit Interface Implementations

    | Improve this Doc View Source

    IEnumerable.GetEnumerator()

    Declaration
    IEnumerator IEnumerable.GetEnumerator()
    Returns
    Type Description
    System.Collections.IEnumerator

    Implements

    System.Collections.Generic.IDictionary<TKey, TValue>
    System.Collections.Generic.ICollection<T>
    System.Collections.Generic.IEnumerable<T>
    System.Collections.IEnumerable

    Extension Methods

    DictionaryExtensions.PutAll<TKey, TValue>(IDictionary<TKey, TValue>, IEnumerable<KeyValuePair<TKey, TValue>>)
    DictionaryExtensions.EntrySet<TKey, TValue>(IDictionary<TKey, TValue>)
    DictionaryExtensions.Put<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)
    EnumerableExtensions.InPairs<T, TOut>(IEnumerable<T>, Func<T, T, TOut>)
    Number.IsNumber(Object)
    SetExtensions.RemoveAll<T>(ICollection<T>, IEnumerable<T>)
    SetExtensions.AddAll<T>(ICollection<T>, IEnumerable<T>)
    • Improve this Doc
    • View Source
    Back to top Copyright © 2019 Licensed to the Apache Software Foundation (ASF)