Libraries.Containers.HashTable Documentation
The HashTable class is a data structure that stores and allows access to items through the use of a key. In the hash table keys and values are paired. Some basic examples and explanations of Lists can be found below.
Example Code
use Libraries.Containers.HashTable
class Main
action Main
//make the hash table
HashTable<text, integer> phoneBook
//add a value(2626984) with a key(Jane)
phoneBook:Add("Jane", 2626984)
//get it back
integer value = phoneBook:GetValue("Jane")
end
end
Inherits from: Libraries.Language.Object
Actions Documentation
Add(Key key, Value value)
This action adds an item to the hash table, given the key-value pair. The implementation of this action is identical to that of Set.
Parameters
- Libraries.Language.Object: The key used to access the value.
- Libraries.Language.Object: The value to be stored.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
Compare(Libraries.Language.Object object)
This action compares two object hash codes and returns an integer. The result is larger if this hash code is larger than the object passed as a parameter, smaller, or equal. In this case, -1 means smaller, 0 means equal, and 1 means larger. This action was changed in Quorum 7 to return an integer, instead of a CompareResult object, because the previous implementation was causing efficiency issues.
Parameters
- Libraries.Language.Object: The object to compare to.
Return
integer: The Compare result, Smaller, Equal, or Larger.
Example
Object o
Object t
integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smaller)
Copy()
This action removes a key-value pair given a key. As the HashTable class requires unique values, this action really only removes the one value
Return
Libraries.Language.Object: True if any key-value pairs were removed and false if none were removed.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
boolean removed = phoneBook:RemoveAllKeys("Jane")
CopyToKeyArray()
This action gets an array that contains all the keys in the hash table. This method requires iterating over all elements in the array, and as such, should be used sparing.
Return
Libraries.Containers.Array: The iterator of keys.
Example
use Libraries.Containers.HashTable
use Libraries.Containers.Iterator
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
Array<text> keysIterator = phoneBook:GetKeyArray()
CopyToValueArray()
This action gets an array that contains all the values in the hash table. This method requires iterating over all elements in the array, and as such, should be used sparingly.
Return
Libraries.Containers.Array: The iterator of keys.
Example
use Libraries.Containers.HashTable
use Libraries.Containers.Iterator
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
Array<text> keysIterator = phoneBook:GetKeyArray()
Empty()
This action empties or clears out the hash table.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
phoneBook:Empty()
Equals(Libraries.Language.Object object)
This action determines if two objects are equal based on their hash code values.
Parameters
- Libraries.Language.Object: The to be compared.
Return
boolean: True if the hash codes are equal and false if they are not equal.
Example
use Libraries.Language.Object
use Libraries.Language.Types.Text
Object o
Text t
boolean result = o:Equals(t)
GetHashCode()
This action gets the hash code for an object.
Return
integer: The integer hash code of the object.
Example
Object o
integer hash = o:GetHashCode()
GetKey(Value value)
This action gets the key that matches the value. Warning: this method is inefficient, you should access items in a hash table through their keys.
Parameters
- Libraries.Language.Object: The value that matches up to a key.
Return
Libraries.Language.Object: The key that matches the key-value pair.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
text key = phoneBook:GetKey(2626984)
GetKeyIterator()
This action gets an iterator that iterates over all the keys in the hash table.
Return
Libraries.Containers.Iterator: The iterator of keys.
Example
use Libraries.Containers.HashTable
use Libraries.Containers.Iterator
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
Iterator<text> keysIterator = phoneBook:GetKeyIterator()
GetSize()
This action gets the number of items in the hash table.
Return
integer: The number of items in the hash table .
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
integer size = phoneBook:GetSize()
GetValue(Key key)
This action gets a value with a given key.
Parameters
- Libraries.Language.Object: The key that matches up to a value.
Return
Libraries.Language.Object: The value that matches the key-value pair.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
integer value = phoneBook:GetValue("Jane")
GetValueIterator()
This action gets an iterator that iterates over all the values in the hash table.
Return
Libraries.Containers.Iterator: The iterator of values.
Example
use Libraries.Containers.HashTable
use Libraries.Containers.Iterator
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
Iterator<integer> keysIterator = phoneBook:GetValueIterator()
HasKey(Key key)
This action determines if the hash table contains a certain key.
Parameters
- Libraries.Language.Object: The key to find.
Return
boolean: True if the key is in the hash table and false if it is not.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
phoneBook:HasKey("Jane")
HasValue(Value value)
This action determines if the hash table contains a certain value.
Parameters
- Libraries.Language.Object: The value to find.
Return
boolean: True if the value is in the hash table and false if it is not.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
phoneBook:HasValue(2626984)
IsEmpty()
This action determines if a hash table is empty.
Return
boolean: True if the hash table is empty and false if it contains any items.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
boolean empty = phoneBook:IsEmpty()
RemoveAllKeys(Key key)
This action removes a key-value pair given a key. As the HashTable class requires unique values, this action really only removes the one value
Parameters
- Libraries.Language.Object: The key of the key-value pair to be removed.
Return
boolean: True if any key-value pairs were removed and false if none were removed.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
boolean removed = phoneBook:RemoveAllKeys("Jane")
RemoveAllValues(Value value)
This action removes all instances of a key-value pair given a key. As the HashTable class requires all values are unique, this only removes at most one value.
Parameters
Return
boolean: True if any key-value pairs were removed and false if none were removed.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
boolean removed = phoneBook:RemoveAllValues(2626984)
RemoveKey(Key key)
This action removes a key-value pair given a key.
Parameters
- Libraries.Language.Object: The key of the key-value pair to be removed.
Return
Libraries.Language.Object: The value that was removed.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
integer value = phoneBook:RemoveKey("Jane")
RemoveValue(Value value)
This action removes a key-value pair given a value.
Parameters
- Libraries.Language.Object: The value of the key-value pair to be removed.
Return
Libraries.Language.Object: The key that was removed.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
text key = phoneBook:RemoveValue(2626984)
Set(Key key, Value value)
This action adds an item to the hash table, given the key-value pair. It functions identically to the add action and exists only as a convenience action, given that there are also actions named with the prefix Get.
Parameters
- Libraries.Language.Object: The key used to access the value.
- Libraries.Language.Object: The value to be stored.
Example
use Libraries.Containers.HashTable
HashTable<text, integer> phoneBook
phoneBook:Add("Jane", 2626984)
SetSize(integer size)
This action sets the size of the hash table. If this table already has entries, it forces it to resize, manually rehashing all objects. For this reason, we recommend selecting a size before adding items to the table.
Parameters
- integer size
Example
use Libraries.Containers.HashTable
use Libraries.Containers.Iterator
HashTable<text, integer> phoneBook
phoneBook:SetSize(20)
phoneBook:Add("Jane", 2626984)
Array<text> keysIterator = phoneBook:GetKeyArray()
On this page
Variables TableAction Documentation- Add(Key key, Value value)
- Compare(Libraries.Language.Object object)
- Copy()
- CopyToKeyArray()
- CopyToValueArray()
- Empty()
- Equals(Libraries.Language.Object object)
- GetHashCode()
- GetKey(Value value)
- GetKeyIterator()
- GetSize()
- GetValue(Key key)
- GetValueIterator()
- HasKey(Key key)
- HasValue(Value value)
- IsEmpty()
- RemoveAllKeys(Key key)
- RemoveAllValues(Value value)
- RemoveKey(Key key)
- RemoveValue(Value value)
- Set(Key key, Value value)
- SetSize(integer size)