Class Hashtable
- All Implemented Interfaces:
AutoCloseable
Store, look up, remove, enumerate, import, and serialize key/value pairs.
Chilkat.Hashtable provides a simple key/value collection for
storing string and integer values by key. It can add or replace entries,
look up values, test whether keys exist, remove entries, count items,
enumerate keys, import URL query parameters, serialize values back to a
query string, and save or load the table in XML format.
String and integer values
Store values as strings or integers using straightforward add, replace, and lookup methods.
Fast key lookup
Check whether a key exists and retrieve its value without manually scanning a list or parsing text.
Add, replace, and remove
Maintain a mutable collection by adding new entries, replacing existing values, removing individual keys, or clearing the table.
Enumerate keys
Retrieve the set of keys when application logic needs to loop through the stored entries.
Query string helpers
Import URL query parameters into the table and serialize the table back to a query-string representation.
XML persistence
Save the hashtable to XML or reload it from XML when values need to be persisted or transferred in a simple structured format.
Common pattern:
Use Hashtable when application code needs a small mutable
key/value table, especially for lookup tables, parsed query parameters, or
simple string/integer settings. Add values with AddStr or
AddInt, retrieve them with LookupStr or
LookupInt, and use XML or query-string methods when the table
needs to be saved, loaded, or converted.
For an extended overview, see Hashtable Class Overview.
</section>
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFromXmlSb(StringBuilder sbXml)Reads hashtable XML previously produced byToXmlSbfrom theStringBuilderpassed inand adds the entries to this table.sbXmlvoidAdds an entry whose string key isand whose value is the integerkey.valuevoidaddQueryParams(String queryParams)Parses URL query parameters fromand adds them to the hashtable as string entries.queryParamsvoidAdds an entry whose string key isand whose value is the stringkey.valuevoidclear()Removes every entry from the hashtable.voidclearWithNewCapacity(int capacity)Removes every entry and reinitializes the hashtable withbuckets.capacitybooleanReturnstrueif the hashtable contains the string key; otherwise returnskeyfalse.booleancontainsIntKey(int key)Returnstrueif the hashtable contains the integer key; otherwise returnskeyfalse.intgetCount()The number of entries currently stored in the hashtable.voidgetKeys(StringTable strTable)Appends all hashtable key strings to theStringTablepassed in.strTableintReturns the integer value associated with the string key.keyReturns the string value associated with the string key.keybooleanRemoves the entry whose string key is.keySerializes the hashtable as a URL query string in the following form:voidtoXmlSb(StringBuilder sbXml)Serializes the current hashtable to Chilkat's hashtable XML format and appends the XML to theStringBuilderpassed in.sbXmlMethods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
Hashtable
public Hashtable()Creates a newHashtableobject.
-
-
Method Details
-
getCount
public int getCount()The number of entries currently stored in the hashtable.Each key contributes one entry regardless of whether its value is a string or an integer. Adding a new key increases
Count; replacing the value of an existing key does not. Removing an entry decreases it, andClearresets it to0.- Returns:
- the value of the
Countproperty (read-only)
-
addFromXmlSb
Reads hashtable XML previously produced byToXmlSbfrom theStringBuilderpassed inand adds the entries to this table.sbXmlThe operation merges with the existing table: keys not present in the XML remain, and an imported entry replaces the current value for the same key. Call
Clearfirst when the XML should completely replace the current contents.Returns
truewhen the XML is parsed and imported successfully; otherwise returnsfalse.- Parameters:
sbXml- thesbXmlargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addInt
Adds an entry whose string key isand whose value is the integerkey. If the key already exists, its previous value is replaced.valueReturns
truewhen the entry is added or replaced. Returnsfalseif the operation cannot be completed, such as because memory could not be allocated.One entry per key: Adding the same key again replaces the existing value; it does not create a second entry. Replacing a value does not increase
Count. UseLookupIntto retrieve an integer value.- Parameters:
key- thekeyargument (notnull)value- thevalueargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addQueryParams
Parses URL query parameters fromand adds them to the hashtable as string entries. Pass the query component in the following form:queryParamsfield1=value1&field2=value2&field3=value3
Each value is expected to be URL encoded and is URL decoded before being stored. If an imported key already exists, its previous value is replaced. Returns
trueon success andfalseif the query parameters cannot be imported.Repeated parameter names: A hashtable stores one value per key. It cannot preserve a query string containing several independent values with the same field name. Use a list-oriented parser when repeated names must remain distinct.
- Parameters:
queryParams- thequeryParamsargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
addStr
Adds an entry whose string key isand whose value is the stringkey. If the key already exists, its previous value is replaced.valueReturns
truewhen the entry is added or replaced. Returnsfalseif the operation cannot be completed, such as because memory could not be allocated.One entry per key: Adding the same key again replaces the existing value; it does not create a second entry. Replacing a value does not increase
Count. UseLookupStrto retrieve a string value.- Parameters:
key- thekeyargument (notnull)value- thevalueargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
clear
public void clear()Removes every entry from the hashtable. After this call,Countis0.The current hash-table capacity is retained. To clear the entries and also select a different bucket capacity, call
ClearWithNewCapacity. -
clearWithNewCapacity
public void clearWithNewCapacity(int capacity)Removes every entry and reinitializes the hashtable withbuckets. The initial default capacity of a newcapacityHashtableobject is521.Returns
trueon success andfalseif the table cannot be reinitialized.Term Meaning
CountThe number of stored entries.Capacity The number of hash buckets, not a maximum entry count.
Performance note: More buckets can reduce collisions for a large table, but use more memory. Choosing roughly twice the expected number of entries and using a prime number are tuning guidelines, not correctness requirements.
- Parameters:
capacity- thecapacityargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
contains
Returnstrueif the hashtable contains the string key; otherwise returnskeyfalse. The result tests key existence regardless of the value stored for that key.Useful before lookup: Call
Containswhen a lookup result could be ambiguous. For example,LookupIntreturns0both for a missing key and for an entry whose value is actually zero.- Parameters:
key- thekeyargument (notnull)- Returns:
- the result; this method never throws
ChilkatException
-
containsIntKey
public boolean containsIntKey(int key)Returnstrueif the hashtable contains the integer key; otherwise returnskeyfalse.Key type: This method accepts an integer key. It is distinct from
Contains, which tests for a string key, and fromAddInt, whose key is a string and whose value is an integer.- Parameters:
key- thekeyargument- Returns:
- the result; this method never throws
ChilkatException
-
getKeys
Appends all hashtable key strings to theStringTablepassed in. Existing strings already contained instrTableare not cleared.strTableReturns
trueon success andfalseon failure.Enumeration order: Hashtable keys may be returned in any order. Do not depend on insertion order or sorted order. Clear the destination
StringTablefirst when it should contain only the current hashtable keys.- Parameters:
strTable- thestrTableargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
lookupInt
Returns the integer value associated with the string key. This method is intended for entries stored withkeyAddInt.If the key does not exist, the return value is
0.Missing key versus zero: Because
0is also a valid stored value, callContainsfirst when the application must distinguish a missing key from an entry containing zero.- Parameters:
key- thekeyargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
lookupStr
Returns the string value associated with the string key. This method is intended for entries stored as strings, including entries added bykeyAddStrorAddQueryParams.Check lookup success: The representation of a failed string-returning call varies by programming language and may be
null,Nothing, or an empty string. An empty string may also be a legitimate value. UseContainsorLastMethodSuccesswhen the distinction matters.- Parameters:
key- thekeyargument (notnull)- Returns:
- the result, or
nullif not found (no exception is thrown in that case)
-
remove
Removes the entry whose string key is.key- Returns
trueif the key existed and the entry was removed. - Returns
falseif the key was not present.
A
falseresult caused by an absent key is a normal lookup outcome, not an indication that the hashtable was damaged. Removing an existing entry decreasesCountby one.- Parameters:
key- thekeyargument (notnull)- Returns:
- the result; this method never throws
ChilkatException
- Returns
-
toQueryString
Serializes the hashtable as a URL query string in the following form:key1=value1&key2=value2&key3=value3
Each value is URL encoded. The returned string does not include a leading
?.Important limitations: Key order is not defined, and a hashtable contains only one value per key. Therefore the output is not suitable when parameter order is significant or repeated field names must be preserved. In language bindings where string-return failure is ambiguous, check
LastMethodSuccess.- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
toXmlSb
Serializes the current hashtable to Chilkat's hashtable XML format and appends the XML to theStringBuilderpassed in. Existing content in thesbXmlStringBuilderis preserved.Returns
trueon success andfalseon failure. The generated XML can be loaded byAddFromXmlSb.<hashtable> <e><k>key</k><v>value</v></e> </hashtable>
Ordering: Because hashtable enumeration order is not defined, do not rely on entries appearing in insertion order or use the serialized text as a canonical, byte-for-byte representation.
- Parameters:
sbXml- thesbXmlargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-