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 implement Comparable
V - values type
All Implemented Interfaces:
Iterable<Map.Entry<K,V>>

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.
  • Constructor Details

    • SequencedMap

      public SequencedMap()
  • Method Details

    • append

      public void append(K key, V value)
      Add a mapping to the end of SequencedMap
      Parameters:
      key - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
      Asymptotic:
      O(1)
    • remove

      public Map.Entry<K,V> remove(K key)
      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

      public V get(K key)
      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 null if map does not contain it
      Asymptotic:
      O(1)
    • size

      public int size()
      Get the size of map
      Returns:
      existing mappings count
    • getPreviousKey

      public K getPreviousKey(K key)
      Get the key of the previous node. If there is no mapping for the key NullPointerException is thrown. Private API: is accessible only to SequencedMapIterator
      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

      public void setPreviousKey(K key, K prev)
      Set the key of the previous node. If there is no mapping for the provided key NullPointerException is thrown. Private API: is accessible only to SequencedMapIterator
      Parameters:
      key - the key of the node whose previous key is being updated
      prev - the key of the node which is to precede the given one
    • contains

      public boolean contains(K key)
      Checks whether a mapping for the given key exists.
      Returns:
      true if the mapping exists otherwise false
    • getHead

      public K getHead()
      Get the key of the first mapping.
      Returns:
      the first node key in the sequence
    • getTail

      public K getTail()
      Get the key of the last mapping.
      Returns:
      the last node key in the sequence
    • iterator

      public SequencedMapForwardIterator<K,V> iterator()
      Get SequencedMapIterator whose iteration starts from the head node
      Specified by:
      iterator in interface Iterable<K extends Comparable<K>>
      Returns:
      iterator that allows to iterate map
    • iterator

      public SequencedMapForwardIterator<K,V> iterator(K key)
      Get SequencedMapForwardIterator whose iteration starts from the provided key if there is a mapping for it otherwise iteration starts from the closest higher element. If key is null than NullPointerException is 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

      public SequencedMapReverseIterator<K,V> reverseIterator()
      Get SequencedMapReverseIterator whose iteration starts from the tail
      Returns:
      iterator that allows to iterate map in reversed order
    • reverseIterator

      public SequencedMapReverseIterator<K,V> reverseIterator(K key)
      Get SequencedMapReverseIterator whose iteration starts from the provided key if there is a mapping for it otherwise iteration starts from the closest lower element. If key is null than NullPointerException is 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

      public void forEach(BiConsumer<? super K,? super V> action)
      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