Class CollectionViews
These features should not be used deeply nested, because the
freqency and time of the evaluation of the contained operations
soon may appear confusing.
When a collection shall be processed element by element,
the genuine Java streams framework
is often preferable.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <A,
B, C extends B>
Map<A, B> castValues
(Class<? extends C> cls, Map<A, C> things) Returns a view on a given collection that modifies the type of each element by casting.static <A,
B> Collection <B> map
(Function<? super A, ? extends B> fun, Collection<? extends A> things) Returns a life view on a given collection that transforms each element by a given function.static <A,
B, C> Map.Entry <A, C> Returns a new map entry with an immutable value from the transformed value of the given entry.static <A,
B, C> Map <A, C> Returns a life view on a given map that transforms for each key the value with a given function.static <A,
B> Map <A, B> Returns a map with a given key set and values computed by a function.static <A,
B, C> Map <B, C> toSpan
(Function<? super A, ? extends B> left, Function<? super A, ? extends C> right, Collection<? extends A> gen) Creates a map by generating entries from generating elements using two functions.static <A,
B, C> Map <B, C> toSpan
(Function<? super A, ? extends B> left, Function<? super A, ? extends C> right, BiFunction<? super C, ? super C, ? extends C> merge, Collection<? extends A> gen) Creates a map by generating entries from generating elements using two functions.
-
Method Details
-
map
public static <A,B> Collection<B> map(Function<? super A, ? extends B> fun, Collection<? extends A> things) Returns a life view on a given collection that transforms each element by a given function.Changes to the backing collection are reflected by the resulting view. Removes through the iterator of the resulting collection are reflected by the backing collection.
- Type Parameters:
A
- the type of given elementsB
- the type of transformed elements- Parameters:
fun
- the transformer functionthings
- the collection to transform- Returns:
- an collection that has, for each current element of
things
, a corresponding element transformed byfun
-
map
Returns a life view on a given map that transforms for each key the value with a given function.Changes to the backing collection are reflected by the resulting view. Removes through the iterator of the resulting collection are reflected by the backing collection. Assignments to the values of an entry from the entry set of the resulting collection are not allowed.
- Type Parameters:
A
- the type of keysB
- the type of given valuesC
- the type of transformed values- Parameters:
fun
- the transformer functionthings
- the map to transform- Returns:
- an unmodifiable map that has, for each current value of
things
, a corresponding value ad-hoc transformed byfun
, for the same key
-
map
Returns a new map entry with an immutable value from the transformed value of the given entry.- Type Parameters:
A
- the type of the keyB
- the type of the given valueC
- the type of the transformed value- Parameters:
fun
- the transformer functione
- a map entry- Returns:
- a map entry that has the same key as
e
and a value resulting from applyingfun
to the value ofe
-
toSpan
public static <A,B, Map<B,C> C> toSpan(Function<? super A, ? extends B> left, Function<? super A, ? extends C> right, Collection<? extends A> gen) Creates a map by generating entries from generating elements using two functions.- Type Parameters:
A
- the type of generating elemensB
- the type of keysC
- the type of values- Parameters:
left
- the key-generating functionright
- the value-generating functiongen
- the generating elements- Returns:
- an unmodifiable map that contains, for each generating element, the computed entry
- Throws:
IllegalArgumentException
- if two generating elements result in the same key but different values
-
toSpan
public static <A,B, Map<B,C> C> toSpan(Function<? super A, ? extends B> left, Function<? super A, ? extends C> right, BiFunction<? super C, ? super C, ? extends C> merge, Collection<? extends A> gen) Creates a map by generating entries from generating elements using two functions.- Type Parameters:
A
- the type of generating elemensB
- the type of keysC
- the type of values- Parameters:
left
- the key-generating functionright
- the value-generating functionmerge
- an operation to reconcile values if two generating elements result in the same keygen
- the generating elements- Returns:
- an unmodifiable map that contains, for each generating element, the computed entry
-
toGraph
Returns a map with a given key set and values computed by a function.- Type Parameters:
A
- the type of keysB
- the type of values- Parameters:
fun
- a function to compute values from keysdom
- the key set- Returns:
- a new map that maps all keys in
dom
to values computed withfun
-
castValues
Returns a view on a given collection that modifies the type of each element by casting.Changes to the underlying collection are reflected by the resulting view.
- Type Parameters:
A
- the type of keysB
- the actual type of elementsC
- the apparent type of elements- Parameters:
cls
- the class to cast tothings
- the map to transform- Returns:
- an unmodifiable map that has, for each current value of
things
, a cast reference of typecls
, for the same key
-