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>
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)
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
|
Constructors
|
Improve this Doc
View Source
HashMap()
Declaration
|
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
|
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
|
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