-
- Type Parameters:
K
- the type for the keyT
- the type for the values
- All Superinterfaces:
Collection<Pair<K,T>>
,Foldable<Pair<K,T>>
,java.lang.Iterable<Pair<K,T>>
,java.io.Serializable
,Streamable<Pair<K,T>>
,Traversable<Pair<K,T>>
,Value<Pair<K,T>>
public interface Map<K,T> extends Collection<Pair<K,T>>
This class represents a map implementation that is immutable. This means all operations that would change its contents result into a newMap
being returned. The current instance is not modified.- Since:
- 0.0.3
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default boolean
containsKey(K key)
Returns true if thisMap
contains the key provided.boolean
containsValue(T value)
Verify if the presented value is contained within theMap
value set.Map<K,T>
filter(java.util.function.Predicate<Pair<K,T>> predicate)
Filter out an element if it does not match the suppliedpredicate
.T
get(K key)
Get the value from theMap
with the corresponding key.Map<K,T>
orElse(java.lang.Iterable<? extends Pair<K,T>> other)
Returns eitherthis
if it is non empty, otherwise will return the providedother
.Map<K,T>
orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends Pair<K,T>>> supplier)
Returns eitherthis
if it is non empty, otherwise the provided supplier is evaluated and returned.Map<K,T>
put(K key, T value)
Add a new entry to theMap
.default Map<K,T>
reject(java.util.function.Predicate<Pair<K,T>> predicate)
Return a list that removes all elements that match thepredicate
provided.Map<K,T>
remove(K key)
Removes any element with the corresponding key from the map.Pair<? extends Map<K,T>,? extends Map<K,T>>
split(java.util.function.Predicate<Pair<K,T>> predicate)
The split operation is an execution that combines theCollection.reject(Predicate)
and theCollection.filter(Predicate)
methods into one.Map<K,T>
tail()
Build a new collection with all elements except for the head.java.util.Map<K,T>
toJava()
Convert the map to a regular Java map type.java.util.stream.Stream<T>
valueStream()
Build a stream of the values contained within thisMap
.-
Methods inherited from interface com.jongsoft.lang.collection.Collection
containsAll, count, first, foldLeft, foldRight, get, head, isEmpty, isSingleValued, last, map, reduceLeft, size, summing
-
Methods inherited from interface com.jongsoft.lang.collection.Traversable
average, iterator, sum
-
-
-
-
Method Detail
-
put
Map<K,T> put(K key, T value)
Add a new entry to theMap
.- Parameters:
key
- the key for the entryvalue
- the value for the entry- Returns:
- the new
Map
instance with the added key, value pair - Throws:
java.lang.NullPointerException
- in case the key isnull
-
remove
Map<K,T> remove(K key)
Removes any element with the corresponding key from the map.- Parameters:
key
- the key indicating what needs to be removed- Returns:
- a new instance without the entry with the key
-
orElse
Map<K,T> orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends Pair<K,T>>> supplier)
Description copied from interface:Traversable
Returns eitherthis
if it is non empty, otherwise the provided supplier is evaluated and returned.- Specified by:
orElse
in interfaceCollection<K>
- Specified by:
orElse
in interfaceTraversable<K>
- Parameters:
supplier
- the supplier to generate the other- Returns:
- this (@code Traversable} if non empty, otherwise other
-
orElse
Map<K,T> orElse(java.lang.Iterable<? extends Pair<K,T>> other)
Description copied from interface:Traversable
Returns eitherthis
if it is non empty, otherwise will return the providedother
.- Specified by:
orElse
in interfaceCollection<K>
- Specified by:
orElse
in interfaceTraversable<K>
- Parameters:
other
- the alternative- Returns:
- this
Traversable
if non empty, orother
-
containsKey
default boolean containsKey(K key)
Returns true if thisMap
contains the key provided.- Parameters:
key
- the key to look for- Returns:
- true if found, otherwise false
-
containsValue
boolean containsValue(T value)
Verify if the presented value is contained within theMap
value set.- Parameters:
value
- the value to look for- Returns:
- if the value is found or not
-
get
T get(K key)
Get the value from theMap
with the corresponding key. If the key is not contained in this instance thennull
will be returned.- Parameters:
key
- the key to obtain the value for- Returns:
- the corresponding value, or
null
-
tail
Map<K,T> tail()
Description copied from interface:Collection
Build a new collection with all elements except for the head. If there is only one element present then an empty collection will be returned. If the operation is called on an empty collection anNoSuchElementException
will be thrown.- Specified by:
tail
in interfaceCollection<K>
- Returns:
- a collection containing the tail
-
valueStream
java.util.stream.Stream<T> valueStream()
Build a stream of the values contained within thisMap
.- Returns:
- the stream with all the values
-
filter
Map<K,T> filter(java.util.function.Predicate<Pair<K,T>> predicate)
Description copied from interface:Streamable
Filter out an element if it does not match the suppliedpredicate
. This operation will iterate over all elements and return a new set containing only the elements where the predicate returnstrue
for.- Specified by:
filter
in interfaceCollection<K>
- Specified by:
filter
in interfaceStreamable<K>
- Specified by:
filter
in interfaceTraversable<K>
- Parameters:
predicate
- the predicate to apply to the contents of this- Returns:
- the filtered value
-
split
Pair<? extends Map<K,T>,? extends Map<K,T>> split(java.util.function.Predicate<Pair<K,T>> predicate)
Description copied from interface:Collection
The split operation is an execution that combines theCollection.reject(Predicate)
and theCollection.filter(Predicate)
methods into one. Separating the values into 2 separate buckets. The first bucket will contain the same as theCollection.filter(Predicate)
operation. The second bucket will contain the result of theCollection.reject(Predicate)
operation.- Specified by:
split
in interfaceCollection<K>
- Parameters:
predicate
- the predicate to use- Returns:
- the two buckets of this operation
-
reject
default Map<K,T> reject(java.util.function.Predicate<Pair<K,T>> predicate)
Description copied from interface:Traversable
Return a list that removes all elements that match thepredicate
provided.- Specified by:
reject
in interfaceCollection<K>
- Specified by:
reject
in interfaceTraversable<K>
- Parameters:
predicate
- the predicate to use- Returns:
- the elements that do not match the
predicate
-
-