T
- Type of the value stored.public final class Trie<T> extends Object
Map
instead.
The empty or null string may be used as a special key to refer to the root node of the trie.
A Trie cannot store null values since null is used as an internal marker to indicate that there is no value associated with a key. This is necessary because the nature of the data structure means that adding a key potentially adds multiple keys many of which will not be associated with a value.
Constructor and Description |
---|
Trie() |
Modifier and Type | Method and Description |
---|---|
void |
add(String key,
T value)
Adds a value to the trie overwriting any existing value
|
boolean |
contains(String key)
Gets whether a key exists in the trie and has a non-null value mapped to
it
|
boolean |
contains(String key,
boolean requireValue)
Gets whether a key exists in the trie and meets the given value criteria
|
boolean |
contains(String key,
T value)
Gets whether a key value pair are present in the trie
|
T |
get(String key)
Gets the value associated with a key
|
T |
longestMatch(String key)
Finds the longest match for a given key i.e.
|
List<T> |
partialSearch(String key)
Performs a search and returns any value associated with any partial or
whole prefix of the key
|
List<T> |
prefixSearch(String prefix)
Performs a prefix search and returns all values mapped under the given
prefix.
|
void |
remove(String key)
Removes a value from the trie
|
T |
shortestMatch(String key)
Finds the shortest match for a given key i.e.
|
public void add(String key, T value)
Note that a null value is treated as if the key does not actually exist
in the tree so trying to add a null is a no-op. If you want to remove a
key use the remove(String)
method instead.
key
- Keyvalue
- Valuepublic void remove(String key)
This doesn't actually remove the key per-se rather sets the value associated with the key to null.
key
- Keypublic boolean contains(String key)
key
- Keypublic boolean contains(String key, boolean requireValue)
key
- KeyrequireValue
- If true a key must have a non-null value associated with it to
be considered to be contained in the tree, if false then the
key must merely map to a node in the triepublic boolean contains(String key, T value)
key
- Keyvalue
- Valuepublic T get(String key)
key
- Keypublic List<T> prefixSearch(String prefix)
partialSearch(String)
method
instead.prefix
- Prefixpublic List<T> partialSearch(String key)
key
- Keypublic T shortestMatch(String key)
key
- KeyLicenced under the Apache License, Version 2.0