Package com.yahoo.collections
Class Hashlet<K,V>
java.lang.Object
com.yahoo.collections.Hashlet<K,V>
Lightweight hash map from key to value with limited
functionality. This class lets you build a map from key to
value. The value for a key may be overwritten and the put and get
methods have the same semantics as for normal Java Maps, but there
is no remove operation. Also, there is no iterator support, but
keys and values can be accessed directly by index. The access order
of keys and values are defined by the insert order of the keys. The
goal of this class is to reduce the amount of object that are
allocated by packing everything into two internal arrays. The keys
and values are packed in an Object array and the hash table and
entries are packed in an int array. The internal arrays are not
created until space is needed. The default initial capacity is 16
entries. If you know you need much more space than this, you can
explicitly reserve more space before starting to insert values. The
maximum load factor is 0.7 and drops slightly with increasing
capacity.
- Author:
- Havard Pettersen
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Obtain the value for a specific key.int
getIndexOfKey
(Object key) Finds the index where the key,value pair is stored.int
hashCode()
key
(int i) Obtain a key.Associate a value with a specific key.void
reserve
(int n) Reserve space for more key value pairs.This will replace the value at the index give.int
size()
The current size.value
(int i) Obtain a value.
-
Constructor Details
-
Hashlet
public Hashlet()Create an empty Hashlet. -
Hashlet
Create a Hashlet that is a shallow copy of another Hashlet.- Parameters:
hashlet
- the Hashlet to copy.
-
-
Method Details
-
reserve
public void reserve(int n) Reserve space for more key value pairs. This method is used by the put method to perform rehashing when needed. It can be invoked directly by the application to reduce the number of rehashes needed to insert a large number of entries.- Parameters:
n
- the number of additional entries to reserve space for
-
size
public int size()The current size. This is the number of key value pairs currently stored in this object.- Returns:
- current size
-
key
Obtain a key. Keys are accessed in the order they were first inserted.- Parameters:
i
- the index of the key, must be in the range [0, size() - 1]- Returns:
- the requested key
-
value
Obtain a value. Values are accessed in the order in which theirs keys were first inserted.- Parameters:
i
- the index of the value, must be in the range [0, size() - 1]- Returns:
- the requested value
-
setValue
This will replace the value at the index give.- Parameters:
i
- the index of the value, must be in the range [0, size() - 1]value
- The new value you want to set for this index.- Returns:
- previous value
-
put
Associate a value with a specific key.- Parameters:
key
- the keyvalue
- the value- Returns:
- the old value for the key, if it was already present
-
get
Obtain the value for a specific key.- Parameters:
key
- the key- Returns:
- the value for a key, or null if not found
-
getIndexOfKey
Finds the index where the key,value pair is stored.- Parameters:
key
- to look for- Returns:
- the index where the key is found or -1 if it is not found
-
hashCode
public int hashCode() -
equals
-