Class SequencedMap<K extends Comparable<K>,V>
java.lang.Object
com.github.fppt.jedismock.datastructures.streams.SequencedMap<K,V>
- Type Parameters:
K- keys type, must implementComparableV- values type
public class SequencedMap<K extends Comparable<K>,V>
extends Object
implements Iterable<Map.Entry<K,V>>
An associative array with O(1) get, delete operations.
Can be interpreted as a sequence of nodes that allows to iterate map.
Can be interpreted as a sequence of nodes that allows to iterate map.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classA node that replaces value inHashMap. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a mapping to the end ofSequencedMapbooleanChecks whether a mapping for the given key exists.voidforEach(BiConsumer<? super K, ? super V> action) Performs the given action for each element of the map.
Method's behaviour:Get the value to which the given key is mappedgetHead()Get the key of the first mapping.getPreviousKey(K key) Get the key of the previous node.getTail()Get the key of the last mapping.iterator()GetSequencedMapIteratorwhose iteration starts from the head nodeGetSequencedMapForwardIteratorwhose iteration starts from the provided key if there is a mapping for it otherwise iteration starts from the closest higher element.Remove the mapping for the given key from map if it existsvoidRemove the mapping for the first key from map if it existsGetSequencedMapReverseIteratorwhose iteration starts from the tailreverseIterator(K key) GetSequencedMapReverseIteratorwhose iteration starts from the provided key if there is a mapping for it otherwise iteration starts from the closest lower element.voidsetPreviousKey(K key, K prev) Set the key of the previous node.intsize()Get the size of mapMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
SequencedMap
public SequencedMap()
-
-
Method Details
-
append
Add a mapping to the end ofSequencedMap- Parameters:
key- the key with which the specified value is to be associatedvalue- the value to be associated with the specified key- Asymptotic:
- O(1)
-
remove
Remove the mapping for the given key from map if it exists- Parameters:
key- the key of mapping to be removed- Returns:
- deleted entry if a mapping for the key exists otherwise
null - Asymptotic:
- O(1) regardless the size of map
-
removeHead
public void removeHead()Remove the mapping for the first key from map if it exists- Asymptotic:
- O(1) regardless the size of map
-
get
Get the value to which the given key is mapped- Parameters:
key- the key whose associated value is to be returned- Returns:
- the value to which the given key is mapped or
nullif map does not contain it - Asymptotic:
- O(1)
-
size
public int size()Get the size of map- Returns:
- existing mappings count
-
getPreviousKey
Get the key of the previous node. If there is no mapping for the keyNullPointerExceptionis thrown. Private API: is accessible only toSequencedMapIterator- Parameters:
key- the key of the node whose previous key is being searched for- Returns:
- the key of the node that precedes the given one
-
setPreviousKey
Set the key of the previous node. If there is no mapping for the provided keyNullPointerExceptionis thrown. Private API: is accessible only toSequencedMapIterator- Parameters:
key- the key of the node whose previous key is being updatedprev- the key of the node which is to precede the given one
-
contains
Checks whether a mapping for the given key exists.- Returns:
trueif the mapping exists otherwisefalse
-
getHead
Get the key of the first mapping.- Returns:
- the first node key in the sequence
-
getTail
Get the key of the last mapping.- Returns:
- the last node key in the sequence
-
iterator
GetSequencedMapIteratorwhose iteration starts from the head node- Specified by:
iteratorin interfaceIterable<K extends Comparable<K>>- Returns:
- iterator that allows to iterate map
-
iterator
GetSequencedMapForwardIteratorwhose iteration starts from the provided key if there is a mapping for it otherwise iteration starts from the closest higher element. IfkeyisnullthanNullPointerExceptionis thrown.- Parameters:
key- the key which is the start of iteration- Returns:
- iterator that points to the provided key or the closest higher key
-
reverseIterator
GetSequencedMapReverseIteratorwhose iteration starts from the tail- Returns:
- iterator that allows to iterate map in reversed order
-
reverseIterator
GetSequencedMapReverseIteratorwhose iteration starts from the provided key if there is a mapping for it otherwise iteration starts from the closest lower element. IfkeyisnullthanNullPointerExceptionis thrown.- Parameters:
key- the key which is the start of iteration- Returns:
- iterator that points to the provided key or the closest lower key
-
forEach
Performs the given action for each element of the map.
Method's behaviour:for (Map.entry<K, V> entry: map) { action.accept(entry.getKey(), entry.getValue()); }- Parameters:
action- function to be executed for each element of the map
-