Set which keeps all elements in prefix trie data structure.
*
/ \
h/ t\
* *
e/ e\
* *
l/ e\
* *
l/ \ \0|
/ p\ *
* *
o/ \0|
* *
\0|
*
Items stored in sorted order.
It implements all methods of Set collection interface except remove.
Complexity is O(L) where L is length of the item.
Adding empty string is not allowed since empty string is a prefix of all possible strings. This would mean that Trie with empty string will match all strings and can cause a confusion.
This collection is not thread safe.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Adding empty string throws an exceptionboolean
iterator()
int
prefixMatches
(String str) Returns length of the longest item from the current trie which is a prefix of a given string.boolean
boolean
removeAll
(Collection<?> c) boolean
int
size()
Methods inherited from class AbstractSet
equals, hashCode
Methods inherited from class AbstractCollection
addAll, clear, containsAll, isEmpty, retainAll, toArray, toArray, toString
Methods inherited from interface Collection
parallelStream, stream, toArray
Methods inherited from interface Set
addAll, clear, containsAll, isEmpty, retainAll, spliterator, toArray, toArray
-
Constructor Details
-
PrefixTrieSet
public PrefixTrieSet()
-
-
Method Details
-
add
Adding empty string throws an exception- Specified by:
add
in interfaceCollection<String>
- Specified by:
add
in interfaceSet<String>
- Overrides:
add
in classAbstractCollection<String>
-
iterator
-
size
public int size()- Specified by:
size
in interfaceCollection<String>
- Specified by:
size
in interfaceSet<String>
- Specified by:
size
in classAbstractCollection<String>
-
remove
- Specified by:
remove
in interfaceCollection<String>
- Specified by:
remove
in interfaceSet<String>
- Overrides:
remove
in classAbstractCollection<String>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<String>
- Specified by:
removeAll
in interfaceSet<String>
- Overrides:
removeAll
in classAbstractSet<String>
-
removeIf
-
contains
- Specified by:
contains
in interfaceCollection<String>
- Specified by:
contains
in interfaceSet<String>
- Overrides:
contains
in classAbstractCollection<String>
-
prefixMatches
Returns length of the longest item from the current trie which is a prefix of a given string. If no such item exist returns 0
-