java.lang.Object
org.jhotdraw8.icollection.RedBlackMap<K,V>
- Type Parameters:
K- the key typeV- the value type
- All Implemented Interfaces:
Serializable,Iterable<Map.Entry<K,,V>> ImmutableMap<K,,V> ImmutableNavigableMap<K,,V> ImmutableSortedMap<K,,V> ReadOnlyMap<K,,V> ReadOnlyNavigableMap<K,,V> ReadOnlySequencedMap<K,,V> ReadOnlySortedMap<K,V>
Implements the
ImmutableNavigableSet interface using a Red-Black tree.
References:
For a similar design, see 'TreeMap.java' in vavr. The internal data structure of this class is licensed from vavr.
- TreeMap.java. Copyright 2023 (c) vavr. MIT License.
- github.com
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedRedBlackMap(@NonNull PrivateData privateData) Creates a new instance with the provided privateData data object. -
Method Summary
Modifier and TypeMethodDescriptionceilingEntry(K k) Returns the least entry in this map with a key greater than or equal to the given key, or null if there is no such entry.intReturns the spliterator characteristics of the key set.clear()Returns a copy of this map that is empty.@Nullable Comparator<? super K> Returns the comparator used to order the keys in this map, or null if this set uses the natural ordering of its keys.booleancontainsKey(@Nullable Object key) Returnstrueif this map contains a entry for the specified key.static <K,V> @NonNull RedBlackMap <K, V> Returns an immutable map that contains the provided elements sorted according to the natural ordering of its elements.static <K,V> @NonNull RedBlackMap <K, V> copyOf(@NonNull Comparator<? super K> comparator, @NonNull Iterable<? extends Map.Entry<? extends K, ? extends V>> c) Returns an immutable map that contains the provided entries, sorted according to the specified comparator.static <K,V> @NonNull RedBlackMap <K, V> Returns an immutable copy of the provided map that contains the provided elements sorted according to the natural ordering of its elements.booleanCompares the specified object with this map for equality.Gets the first entry in this map ornullif this map is empty.floorEntry(K k) Returns the greatest entry in this map with a key less than or equal to the given key, or null if there is no such entry.Returns the value to which the key is mapped, ornullif this map contains no entry for the key.inthashCode()Returns the hash code value for this map.higherEntry(K k) Returns the least entry in this map with a key greater than the given key, or null if there is no such entry.booleanisEmpty()Returnstrueif this map contains no entries.iterator()Returns an iterator over the entries contained in this map.Gets the last entry in this map ornullif this map is empty.lowerEntry(K k) Returns the greatest entry in this map with a key less than the given key, or null if there is no such entry.intmaxSize()Returns the maximal number of entries that this map type can holdprotected @NonNull RedBlackMap<K, V> newInstance(@NonNull PrivateData privateData) Creates a new instance with the provided privateData object as its internal data structure.static <K,V> @NonNull RedBlackMap <K, V> of()Returns an empty immutable map, sorted according to the natural ordering of its entries.static <K,V> @NonNull RedBlackMap <K, V> Returns an immutable map that contains the provided entries, sorted according to the natural ordering of its entries.Returns a copy of this map that contains all entries of this map with the specified entry added or updated.Returns a copy of this map that contains all entries of this map with entries from the specified map added or updated.Returns a copy of this map that contains all entries of this map with entries from the specified map added or updated.putKeyValues(@NonNull Object @NonNull ... kv) Returns a copy of this map that contains all entries of this map with entries from the specified map added or updated.Returns a reversed-order view of this map.Returns a copy of this map that contains all entries of this map with the specified entry removed.Returns a copy of this map that contains all entries of this map except the entries of the specified collection.Returns a copy of this map that contains only entries that are in this map and in the specified collection.retainAll(@NonNull ReadOnlyCollection<? extends K> c) Returns a copy of this map that contains only entries that are in this map and in the specified collection.intsize()Returns the number of entries contained in this map..static <K,V> @NonNull RedBlackMap <K, V> sortedOf(@Nullable Comparator<? super K> comparator) Returns an empty immutable map, sorted according to the specified comparator.static <K,V> @NonNull RedBlackMap <K, V> sortedOf(@Nullable Comparator<? super K> comparator, @NonNull Map.Entry<K, V> @Nullable ... elements) Returns an immutable map that contains the provided elements, sorted according to the specified comparator.Returns a mutable copy of this map.toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlyMap
containsEntry, containsValue, getOrDefaultMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlyNavigableMap
ceilingKey, floorKey, higherKey, lowerKeyMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySequencedMap
asMap, readOnlyEntrySet, readOnlyKeySet, readOnlyValuesMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySortedMap
spliterator
-
Constructor Details
-
RedBlackMap
Creates a new instance with the provided privateData data object.This constructor is intended to be called from a constructor of the subclass, that is called from method
newInstance(PrivateData).- Parameters:
privateData- an privateData data object
-
-
Method Details
-
newInstance
Creates a new instance with the provided privateData object as its internal data structure.Subclasses must override this method, and return a new instance of their subclass!
- Parameters:
privateData- the internal data structure needed by this class for creating the instance.- Returns:
- a new instance of the subclass
-
copyOf
public static <K,V> @NonNull RedBlackMap<K,V> copyOf(@NonNull Comparator<? super K> comparator, @NonNull Iterable<? extends Map.Entry<? extends K, ? extends V>> c) Returns an immutable map that contains the provided entries, sorted according to the specified comparator.- Type Parameters:
K- the key typeV- the value type- Parameters:
c- an iterable- Returns:
- an immutable map of the provided elements
-
putAll
Description copied from interface:ImmutableMapReturns a copy of this map that contains all entries of this map with entries from the specified map added or updated.- Specified by:
putAllin interfaceImmutableMap<K,V> - Specified by:
putAllin interfaceImmutableNavigableMap<K,V> - Specified by:
putAllin interfaceImmutableSortedMap<K,V> - Parameters:
m- another map- Returns:
- this map instance if it already contains the same entries, or a different map instance with the entries added or updated
-
putAll
public @NonNull RedBlackMap<K,V> putAll(@NonNull Iterable<? extends Map.Entry<? extends K, ? extends V>> c) Description copied from interface:ImmutableMapReturns a copy of this map that contains all entries of this map with entries from the specified map added or updated.- Specified by:
putAllin interfaceImmutableMap<K,V> - Specified by:
putAllin interfaceImmutableNavigableMap<K,V> - Specified by:
putAllin interfaceImmutableSortedMap<K,V> - Parameters:
c- another map- Returns:
- this map instance if it already contains the same entries, or a different map instance with the entries added or updated
-
putKeyValues
Description copied from interface:ImmutableMapReturns a copy of this map that contains all entries of this map with entries from the specified map added or updated.- Specified by:
putKeyValuesin interfaceImmutableMap<K,V> - Specified by:
putKeyValuesin interfaceImmutableNavigableMap<K,V> - Specified by:
putKeyValuesin interfaceImmutableSortedMap<K,V> - Parameters:
kv- a list of alternating keys and values- Returns:
- this map instance if it already contains the same entries, or a different map instance with the entries added or updated
-
removeAll
Description copied from interface:ImmutableMapReturns a copy of this map that contains all entries of this map except the entries of the specified collection.- Specified by:
removeAllin interfaceImmutableMap<K,V> - Specified by:
removeAllin interfaceImmutableNavigableMap<K,V> - Specified by:
removeAllin interfaceImmutableSortedMap<K,V> - Parameters:
c- a collection with keys of entries to be removed from this map- Returns:
- this map instance if it already does not contain the entries, or a different map instance with the entries removed
-
retainAll
Description copied from interface:ImmutableMapReturns a copy of this map that contains only entries that are in this map and in the specified collection.- Specified by:
retainAllin interfaceImmutableMap<K,V> - Specified by:
retainAllin interfaceImmutableNavigableMap<K,V> - Specified by:
retainAllin interfaceImmutableSortedMap<K,V> - Parameters:
c- a collection with keys of entries to be retained in this map- Returns:
- this map instance if it has not changed, or a different map instance with entries removed
-
retainAll
Description copied from interface:ImmutableMapReturns a copy of this map that contains only entries that are in this map and in the specified collection.- Specified by:
retainAllin interfaceImmutableMap<K,V> - Specified by:
retainAllin interfaceImmutableNavigableMap<K,V> - Specified by:
retainAllin interfaceImmutableSortedMap<K,V> - Parameters:
c- a collection with keys of entries to be retained in this map- Returns:
- this map instance if it has not changed, or a different map instance with entries removed
-
copyOf
public static <K,V> @NonNull RedBlackMap<K,V> copyOf(@NonNull Iterable<? extends Map.Entry<? extends K, ? extends V>> c) Returns an immutable map that contains the provided elements sorted according to the natural ordering of its elements.- Type Parameters:
K- the key typeV- the value type- Parameters:
c- an iterable- Returns:
- an immutable map of the provided elements
-
copyOf
Returns an immutable copy of the provided map that contains the provided elements sorted according to the natural ordering of its elements.- Type Parameters:
K- the key typeV- the value type- Parameters:
map- a map- Returns:
- an immutable copy
-
sortedOf
Returns an empty immutable map, sorted according to the specified comparator.- Type Parameters:
K- the key typeV- the value type- Returns:
- an empty immutable map
-
sortedOf
@SafeVarargs public static <K,V> @NonNull RedBlackMap<K,V> sortedOf(@Nullable Comparator<? super K> comparator, @NonNull Map.Entry<K, V> @Nullable ... elements) Returns an immutable map that contains the provided elements, sorted according to the specified comparator.- Type Parameters:
K- the key typeV- the value type- Parameters:
elements- elements- Returns:
- an immutable map of the provided elements
-
of
Returns an empty immutable map, sorted according to the natural ordering of its entries.- Type Parameters:
K- the key typeV- the value type- Returns:
- an empty immutable map
-
of
@SafeVarargs public static <K,V> @NonNull RedBlackMap<K,V> of(@NonNull Map.Entry<K, V> @Nullable ... entries) Returns an immutable map that contains the provided entries, sorted according to the natural ordering of its entries.- Type Parameters:
K- the key typeV- the value type- Parameters:
entries- entries- Returns:
- an immutable map of the provided entries
-
ceilingEntry
Description copied from interface:ReadOnlyNavigableMapReturns the least entry in this map with a key greater than or equal to the given key, or null if there is no such entry.- Specified by:
ceilingEntryin interfaceReadOnlyNavigableMap<K,V> - Parameters:
k- the given key- Returns:
- ceiling entry or null
-
comparator
Description copied from interface:ReadOnlySortedMapReturns the comparator used to order the keys in this map, or null if this set uses the natural ordering of its keys.- Specified by:
comparatorin interfaceReadOnlySortedMap<K,V> - Returns:
- comparator or null
-
floorEntry
Description copied from interface:ReadOnlyNavigableMapReturns the greatest entry in this map with a key less than or equal to the given key, or null if there is no such entry.- Specified by:
floorEntryin interfaceReadOnlyNavigableMap<K,V> - Parameters:
k- the given key- Returns:
- floor entry or null
-
higherEntry
Description copied from interface:ReadOnlyNavigableMapReturns the least entry in this map with a key greater than the given key, or null if there is no such entry.- Specified by:
higherEntryin interfaceReadOnlyNavigableMap<K,V> - Parameters:
k- the given key- Returns:
- higher entry or null
-
lowerEntry
Description copied from interface:ReadOnlyNavigableMapReturns the greatest entry in this map with a key less than the given key, or null if there is no such entry.- Specified by:
lowerEntryin interfaceReadOnlyNavigableMap<K,V> - Parameters:
k- the given key- Returns:
- lower entry or null
-
firstEntry
Description copied from interface:ReadOnlySequencedMapGets the first entry in this map ornullif this map is empty.- Specified by:
firstEntryin interfaceReadOnlySequencedMap<K,V> - Returns:
- the first entry or
null
-
lastEntry
Description copied from interface:ReadOnlySequencedMapGets the last entry in this map ornullif this map is empty.- Specified by:
lastEntryin interfaceReadOnlySequencedMap<K,V> - Returns:
- the last entry or
null
-
clear
Description copied from interface:ImmutableMapReturns a copy of this map that is empty.- Specified by:
clearin interfaceImmutableMap<K,V> - Specified by:
clearin interfaceImmutableNavigableMap<K,V> - Specified by:
clearin interfaceImmutableSortedMap<K,V> - Returns:
- this set instance if it is already empty, or a different set instance that is empty.
-
isEmpty
public boolean isEmpty()Description copied from interface:ReadOnlyMapReturnstrueif this map contains no entries.- Specified by:
isEmptyin interfaceReadOnlyMap<K,V> - Returns:
trueif empty
-
readOnlyReversed
Description copied from interface:ReadOnlySequencedMapReturns a reversed-order view of this map.Changes to the underlying map are visible in the reversed view.
- Specified by:
readOnlyReversedin interfaceReadOnlySequencedMap<K,V> - Returns:
- a reversed-order view of this map
-
size
public int size()Description copied from interface:ReadOnlyMapReturns the number of entries contained in this map..- Specified by:
sizein interfaceReadOnlyMap<K,V> - Returns:
- the number of entries
-
get
Description copied from interface:ReadOnlyMapReturns the value to which the key is mapped, ornullif this map contains no entry for the key.- Specified by:
getin interfaceReadOnlyMap<K,V> - Parameters:
key- a key- Returns:
- the mapped value or
null
-
containsKey
Description copied from interface:ReadOnlyMapReturnstrueif this map contains a entry for the specified key.- Specified by:
containsKeyin interfaceReadOnlyMap<K,V> - Parameters:
key- a key- Returns:
trueif this map contains a entry for the specified key
-
iterator
Description copied from interface:ReadOnlyMapReturns an iterator over the entries contained in this map. -
put
Description copied from interface:ImmutableMapReturns a copy of this map that contains all entries of this map with the specified entry added or updated.- Specified by:
putin interfaceImmutableMap<K,V> - Specified by:
putin interfaceImmutableNavigableMap<K,V> - Specified by:
putin interfaceImmutableSortedMap<K,V> - Parameters:
key- the key of the entryvalue- the value of the entry- Returns:
- this map instance if it already contains the same entry, or a different map instance with the entry added or updated
-
remove
Description copied from interface:ImmutableMapReturns a copy of this map that contains all entries of this map with the specified entry removed.- Specified by:
removein interfaceImmutableMap<K,V> - Specified by:
removein interfaceImmutableNavigableMap<K,V> - Specified by:
removein interfaceImmutableSortedMap<K,V> - Parameters:
key- the key of the entry- Returns:
- this map instance if it already does not contain the entry, or a different map instance with the entry removed
-
toMutable
Description copied from interface:ImmutableMapReturns a mutable copy of this map.- Specified by:
toMutablein interfaceImmutableMap<K,V> - Returns:
- a mutable copy.
-
maxSize
public int maxSize()Description copied from interface:ImmutableMapReturns the maximal number of entries that this map type can hold- Specified by:
maxSizein interfaceImmutableMap<K,V> - Returns:
- the maximal size
-
equals
Description copied from interface:ReadOnlyMapCompares the specified object with this map for equality.Returns
trueif the given object is also a read-only map and the two maps represent the same entries, ignorig the sequence of the map entries. -
hashCode
public int hashCode()Description copied from interface:ReadOnlyMapReturns the hash code value for this map. The hash code is the sum of the hash code of its entries. -
toString
-
characteristics
public int characteristics()Description copied from interface:ReadOnlySequencedMapReturns the spliterator characteristics of the key set. This implementation returnsSpliterator.SIZED|Spliterator.DISTINCT.- Specified by:
characteristicsin interfaceReadOnlyMap<K,V> - Specified by:
characteristicsin interfaceReadOnlySequencedMap<K,V> - Specified by:
characteristicsin interfaceReadOnlySortedMap<K,V> - Returns:
- characteristics.
-