Package org.javimmutable.collections
Interface IMap<K,V>
- All Superinterfaces:
ICollection<IMapEntry<K,
,V>> InvariantCheckable
,IStreamable<IMapEntry<K,
,V>> Iterable<IMapEntry<K,
,V>> Mapped<K,
,V> Serializable
,SplitableIterable<IMapEntry<K,
V>>
- All Known Implementing Classes:
AbstractMap
,EmptyHashMap
,HashMap
,OrderedMap
,TreeMap
@Immutable
public interface IMap<K,V>
extends ICollection<IMapEntry<K,V>>, Mapped<K,V>, InvariantCheckable, Serializable
Interface for immutable data structures that allow storage and retrieval of
key/value pairs. null is always an allowed value within the map but is not
an allowed key.
-
Method Summary
Modifier and TypeMethodDescriptionSets the value associated with a specific key.Copies all key-value pairs from the given map.Copies all key-value pairs from the given map.Deletes the entry for the specified key (if any).Search for a value within the map and return a Holder indicating if the value was found and, if it was found, the value itself.Search for an Entry within the map and return a Holder indicating if the Entry was found and, if it was found, the Entry itself.default void
Processes every key/value pair in this map using the provided function.default <E extends Exception>
voidforEachThrows
(Proc2Throws<K, V, E> proc) Processes every key/value pair in this map using the provided function.getMap()
Creates an unmodifiable java.util.Map reflecting the values of thisIMap
.Add key/value entry to the map, replacing any existing entry with same key.Adds the values to the end of the list in the same order they appear in the Iterable.Adds the values to the end of the list in the same order they appear in the Iterable.keys()
Creates an IStreamable to access all of the Map's keys.Creates a Builder with the same type signature as this Map.Returns a Collector that creates a set of the same type as this containing all of the collected values inserted over whatever starting values this already contained.default <R> R
Processes every key value pair in this map using the provided function to produce a value.default <R,
E extends Exception>
RreduceThrows
(R sum, Sum2Throws<K, V, R, E> proc) Processes every key value pair in this map using the provided function to produce a value.Returns a map of the same type as this containing all those elements for which predicate returns false.Returns a map of the same type as this containing only those elements for which predicate returns true.Update the value at the key.values()
Creates an IStreamable to access all of the Map's values.Methods inherited from interface org.javimmutable.collections.ICollection
isEmpty, isNonEmpty, size
Methods inherited from interface org.javimmutable.collections.InvariantCheckable
checkInvariants
Methods inherited from interface org.javimmutable.collections.IStreamable
getSpliteratorCharacteristics, iterator, parallelStream, spliterator, stream
Methods inherited from interface org.javimmutable.collections.Mapped
get, getValueOr
Methods inherited from interface org.javimmutable.collections.SplitableIterable
forEachThrows, indexedForEach, indexedForEachThrows, reduce, reduceThrows
-
Method Details
-
insert
Add key/value entry to the map, replacing any existing entry with same key.- Specified by:
insert
in interfaceICollection<K>
-
insertAll
Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list.- Specified by:
insertAll
in interfaceICollection<K>
- Returns:
- instance of list containing the collection
-
insertAll
Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list.- Specified by:
insertAll
in interfaceICollection<K>
- Returns:
- instance of list containing the collection
-
find
Search for a value within the map and return a Holder indicating if the value was found and, if it was found, the value itself. Holder allows null values to be returned unambiguously. -
findEntry
Search for an Entry within the map and return a Holder indicating if the Entry was found and, if it was found, the Entry itself.- Parameters:
key
- non-null key to search for- Returns:
- empty Holder if not found, otherwise filled Holder with Entry
-
assign
Sets the value associated with a specific key. Key must be non-null but value can be null. If the key already has a value in the map the old value is discarded and the new value is stored in its place. Returns a newIMap
reflecting any changes. The original map is always left unchanged.- Parameters:
key
- non-null keyvalue
- possibly null value- Returns:
- new map reflecting the change
-
assignAll
Copies all key-value pairs from the given map. The map itself and its keys must be nonnull, but values can be null. If a key already has a value in the map, the old value is replaced with the new value. Returns a newIMap
with the changes.- Parameters:
map
-IMap
to take values from- Returns:
- new map reflecting the change
-
assignAll
Copies all key-value pairs from the given map. The map itself and its keys must be nonnull, but values can be null. If a key already has a value in the map, the old value is replaced with the new value. Returns a newIMap
with the changes.- Parameters:
map
- Map to take values from- Returns:
- new map reflecting the change
-
delete
Deletes the entry for the specified key (if any). Returns a new map if the value was deleted or the current map if the key was not contained in the map.- Parameters:
key
- non-null key- Returns:
- same or different map depending on whether key was removed
-
deleteAll
- Specified by:
deleteAll
in interfaceICollection<K>
- Returns:
- an equivalent collection with no values
-
getMap
Creates an unmodifiable java.util.Map reflecting the values of thisIMap
.- Returns:
- Map view of this
IMap
-
keys
Creates an IStreamable to access all of the Map's keys. -
values
Creates an IStreamable to access all of the Map's values. -
mapBuilder
Creates a Builder with the same type signature as this Map. -
mapCollector
Returns a Collector that creates a set of the same type as this containing all of the collected values inserted over whatever starting values this already contained. -
update
Update the value at the key. A Holder containing the value currently stored at the key, or an empty Holder if the key is not currently bound, is passed to the generator function.- Parameters:
key
- non-null keygenerator
- function to call with current value to create value if key is already bound- Returns:
- new map with changes applied
-
forEach
Processes every key/value pair in this map using the provided function. -
forEachThrows
Processes every key/value pair in this map using the provided function.- Throws:
E
-
reduce
Processes every key value pair in this map using the provided function to produce a value.- Type Parameters:
R
- type of the sum- Parameters:
sum
- initial value for process (used with first key/value pair of map)proc
- function to combine a sum with a key value pair to produce a new sum- Returns:
- final value (or initial value if this map is empty)
-
reduceThrows
Processes every key value pair in this map using the provided function to produce a value.- Type Parameters:
R
- type of the sumE
- type of the Exception thrown by the function- Parameters:
sum
- initial value for process (used with first key/value pair of map)proc
- function to combine a sum with a key value pair to produce a new sum- Returns:
- final value (or initial value if this map is empty)
- Throws:
E
-
select
Returns a map of the same type as this containing only those elements for which predicate returns true. Implementations are optimized assuming predicate will return false more often than true.- Parameters:
predicate
- decides whether to include an element- Returns:
- map of same type as this containing only those elements for which predicate returns true
-
reject
Returns a map of the same type as this containing all those elements for which predicate returns false. Implementations can be optimized assuming predicate will return false more often than true.- Parameters:
predicate
- decides whether to include an element- Returns:
- map of same type as this containing only those elements for which predicate returns false
-