Interface PrefixMap<V>
- Type Parameters:
V- value type
- All Known Implementing Classes:
StringPrefixMap
The PrefixMap is a key-value type collection where the key is assumed to be the prefix of the String for which later an answer is requested.
So the retrieval of the value is based on a startsWith() check of the requested input
against each of the stored prefixes. The value associated with the selected
prefix is then returned.
An example use case is where mobile phone device models of a certain brand all start with the same substring. This data structure can be used to efficiently retrieve the best fitting prefix to find the associated value (i.e. the brand of the device).
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidclear()The prefixmap will be empty after this call returns.default booleandefault booleancontainsPrefix(int[] prefix) Returnstrueif this map contains an exact mapping for the specified prefix.default booleancontainsPrefix(String prefix) Returnstrueif this map contains an exact mapping for the specified prefix.booleanReturnstrueif this map contains an exact mapping for the specified prefix.default booleandefault VReturn the value of theexactmatching prefix.Return the value of theexactmatching prefix.getAllMatches(int[] input) Returns List of all matches that have a value.getAllMatches(String input) Returns List of all matches that have a value.Returns List of all matches that have a value.default VgetLongestMatch(int[] input) Return the value of the longest matching prefix.default VgetLongestMatch(String input) Return the value of the longest matching prefix.Return the value of the longest matching prefix.default VgetShortestMatch(int[] input) Return the value of theshortestmatching prefix.default VgetShortestMatch(String input) Return the value of theshortestmatching prefix.Return the value of theshortestmatching prefix.default booleanisEmpty()Return true if empty.keySet()Stored the specified value as the result for the specified prefix.default voidCopies all of the mappings from the specified map to this prefixmap.default Vdefault VRemoves the mapping for a prefix if present.intsize()Return the number of prefix-value mappings in this prefixmap.default Collection<V> values()Methods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, entrySet, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Method Details
-
size
-
isEmpty
-
putAll
Copies all of the mappings from the specified map to this prefixmap.- Specified by:
putAllin interfaceMap<String,V> - Parameters:
prefixesAndValues- mappings to be stored in this map- Throws:
NullPointerException- if one or more of the prefixes or values are null. If this happens the PrefixMap is in an undefined state.
-
put
Stored the specified value as the result for the specified prefix.If for the specified prefix previously a value was present then the old value is replaced with the new value and the old value is returned.
- Specified by:
putin interfaceMap<String,V> - Parameters:
prefix- prefix with which the specified value is to be associatedvalue- value to be associated with the specified prefix- Returns:
- the previous value of the specified
prefix, ornullif there was no mapping forprefix. - Throws:
NullPointerException- if either the prefix or value are null. If this happens the PrefixMap is unchanged.
-
remove
Removes the mapping for a prefix if present.- Parameters:
prefix- prefix whose mapping is to be removed from the prefixmap- Returns:
- the previous value associated with
prefix(may be null). - Throws:
UnsupportedOperationException- if not implemented.
-
remove
-
containsKey
- Specified by:
containsKeyin interfaceMap<String,V>
-
containsValue
- Specified by:
containsValuein interfaceMap<String,V>
-
get
-
get
Return the value of theexactmatching prefix.The value returned is the stored prefix for which is true:
input.equals(prefix). -
keySet
-
values
-
clear
default void clear()The prefixmap will be empty after this call returns.- Specified by:
clearin interfaceMap<String,V> - Throws:
UnsupportedOperationException- if theclearoperation is not supported by this map
-
containsPrefix
Returnstrueif this map contains an exact mapping for the specified prefix.- Parameters:
prefix- prefix whose presence in this prefixmap is to be checked- Returns:
trueif this map contains an the exact mapping for the specified prefix.
-
containsPrefix
default boolean containsPrefix(int[] prefix) Returnstrueif this map contains an exact mapping for the specified prefix.- Parameters:
prefix- prefix whose presence in this prefixmap is to be checked- Returns:
trueif this map contains an the exact mapping for the specified prefix.
-
containsPrefix
Returnstrueif this map contains an exact mapping for the specified prefix.- Parameters:
prefix- prefix whose presence in this prefixmap is to be checked- Returns:
trueif this map contains an the exact mapping for the specified prefix.
-
getShortestMatch
-
getShortestMatch
Return the value of theshortestmatching prefix.The value returned is the shortest stored prefix for which is true:
input.startsWith(prefix).- Parameters:
input- The string (iterator of Unicode CodePoints) for which we need value of the stored prefix.- Returns:
- The value, null if not found.
-
getShortestMatch
Return the value of theshortestmatching prefix.The value returned is the shortest stored prefix for which is true:
input.startsWith(prefix).- Parameters:
input- The string (iterator of Unicode CodePoints) for which we need value of the stored prefix.- Returns:
- The value, null if not found.
-
getLongestMatch
-
getLongestMatch
Return the value of the longest matching prefix.The value returned is the longest stored prefix for which is true:
input.startsWith(prefix).- Parameters:
input- The string (iterator of Unicode CodePoints) for which we need value of the stored prefix.- Returns:
- The value, null if not found.
-
getLongestMatch
Return the value of the longest matching prefix.The value returned is the longest stored prefix for which is true:
input.startsWith(prefix).- Parameters:
input- The string (iterator of Unicode CodePoints) for which we need value of the stored prefix.- Returns:
- The value, null if not found.
-
getAllMatches
Returns List of all matches that have a value.The list contains all non-null values for the prefix values where this is true:
input.startsWith(prefix).- Parameters:
input- The string for which we need value of the stored prefix- Returns:
- The list of values, an empty List if nothing is found.
-
getAllMatches
Returns List of all matches that have a value.The list contains all non-null values for the prefix values where this is true:
input.startsWith(prefix).- Parameters:
input- The string (iterator of Unicode CodePoints) for which we need value of the stored prefix.- Returns:
- The list of values, an empty List if nothing is found.
-
getAllMatches
Returns List of all matches that have a value.The list contains all non-null values for the prefix values where this is true:
input.startsWith(prefix).- Parameters:
input- The string (iterator of Unicode CodePoints) for which we need value of the stored prefix.- Returns:
- The list of values, an empty List if nothing is found.
-