类 SeparateChainingHashST<Key,Value>
- java.lang.Object
-
- edu.princeton.cs.algs4.SeparateChainingHashST<Key,Value>
-
public class SeparateChainingHashST<Key,Value> extends java.lang.Object
TheSeparateChainingHashST
class represents a symbol table of generic key-value pairs. It supports the usual put, get, contains, delete, size, and is-empty methods. It also provides a keys method for iterating over all of the keys. A symbol table implements the associative array abstraction: when associating a value with a key that is already in the symbol table, the convention is to replace the old value with the new value. UnlikeMap
, this class uses the convention that values cannot benull
—setting the value associated with a key tonull
is equivalent to deleting the key from the symbol table.This implementation uses a separate chaining hash table. It requires that the key type overrides the
equals()
andhashCode()
methods. The expected time per put, contains, or remove operation is constant, subject to the uniform hashing assumption. The size, and is-empty operations take constant time. Construction takes constant time.For additional documentation, see Section 3.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. For other implementations, see
ST
,BinarySearchST
,SequentialSearchST
,BST
,RedBlackBST
, andLinearProbingHashST
,
-
-
构造器概要
构造器 构造器 说明 SeparateChainingHashST()
Initializes an empty symbol table.SeparateChainingHashST(int m)
Initializes an empty symbol table withm
chains.
-
方法概要
修饰符和类型 方法 说明 boolean
contains(Key key)
Returns true if this symbol table contains the specified key.void
delete(Key key)
Removes the specified key and its associated value from this symbol table (if the key is in this symbol table).Value
get(Key key)
Returns the value associated with the specified key in this symbol table.boolean
isEmpty()
Returns true if this symbol table is empty.java.lang.Iterable<Key>
keys()
static void
main(java.lang.String[] args)
Unit tests theSeparateChainingHashST
data type.void
put(Key key, Value val)
Inserts the specified key-value pair into the symbol table, overwriting the old value with the new value if the symbol table already contains the specified key.int
size()
Returns the number of key-value pairs in this symbol table.
-
-
-
方法详细资料
-
size
public int size()
Returns the number of key-value pairs in this symbol table.- 返回:
- the number of key-value pairs in this symbol table
-
isEmpty
public boolean isEmpty()
Returns true if this symbol table is empty.- 返回:
true
if this symbol table is empty;false
otherwise
-
contains
public boolean contains(Key key)
Returns true if this symbol table contains the specified key.- 参数:
key
- the key- 返回:
true
if this symbol table containskey
;false
otherwise- 抛出:
java.lang.IllegalArgumentException
- ifkey
isnull
-
get
public Value get(Key key)
Returns the value associated with the specified key in this symbol table.- 参数:
key
- the key- 返回:
- the value associated with
key
in the symbol table;null
if no such value - 抛出:
java.lang.IllegalArgumentException
- ifkey
isnull
-
put
public void put(Key key, Value val)
Inserts the specified key-value pair into the symbol table, overwriting the old value with the new value if the symbol table already contains the specified key. Deletes the specified key (and its associated value) from this symbol table if the specified value isnull
.- 参数:
key
- the keyval
- the value- 抛出:
java.lang.IllegalArgumentException
- ifkey
isnull
-
delete
public void delete(Key key)
Removes the specified key and its associated value from this symbol table (if the key is in this symbol table).- 参数:
key
- the key- 抛出:
java.lang.IllegalArgumentException
- ifkey
isnull
-
keys
public java.lang.Iterable<Key> keys()
-
main
public static void main(java.lang.String[] args)
Unit tests theSeparateChainingHashST
data type.- 参数:
args
- the command-line arguments
-
-