Skip to content
Snippets Groups Projects
Commit cf982027 authored by Alexander Diewald's avatar Alexander Diewald
Browse files

- Add mapping lambda util methods that ease the in- and output operations of a...

- Add mapping lambda util methods that ease the in- and output operations of a mapped stream (less writing).
- Add a lambda util method for flatmap operations.
refs 2779
parent 59a48886
No related branches found
No related tags found
No related merge requests found
......@@ -153,7 +153,7 @@ public class LambdaUtils {
/**
* Applies the mapping {@link Function} {@code mapper} to the given collection and returns the
* result as a list.
* result as a collection whose implementation is given by the {@link Supplier}.
*
* @param inColl
* Collection of input elements.
......@@ -163,8 +163,56 @@ public class LambdaUtils {
* Supplier for the output {@link Collection}, e.g. TreeSet::new.
* @return Collection of mapping results.
*/
public static <S, T> Collection<S> map(Collection<T> inColl, Function<T, S> mapper,
public static <S, T> Collection<S> mapInOut(Collection<T> inColl, Function<T, S> mapper,
Supplier<? extends Collection<S>> sup) {
return inColl.parallelStream().map(mapper).collect(Collectors.toCollection(sup));
}
/**
* Applies the mapping {@link Function} {@code mapper} to the given collection and returns the
* result as a {@code Stream}.
*
* @param inColl
* Collection of input elements.
* @param mapper
* Mapping function.
* @return Stream of the mapping result.
*/
public static <S, T> Stream<S> mapIn(Collection<T> inColl, Function<T, S> mapper) {
return inColl.parallelStream().map(mapper);
}
/**
* Applies the mapping {@link Function} {@code mapper} to the given {@code stream} and returns
* the result as a collection whose implementation is given by the {@link Supplier}.
*
* @param stream
* Stream of input elements.
* @param mapper
* Mapping function.
* @param sup
* Supplier for the output {@link Collection}, e.g. TreeSet::new.
* @return Collection of mapping results.
*/
public static <S, T> Collection<S> mapOut(Stream<T> stream, Function<T, S> mapper,
Supplier<? extends Collection<S>> sup) {
return stream.map(mapper).collect(Collectors.toCollection(sup));
}
/**
* Applies the flat mapping {@link Function} {@code mapper} to the given collection and returns
* the result as a collection whose implementation is given by the {@link Supplier}.
*
* @param inColl
* Collection of input elements.
* @param mapper
* Mapping function.
* @param sup
* Supplier for the output {@link Collection}, e.g. TreeSet::new.
* @return Collection of mapping results.
*/
public static <S, T, R extends Stream<S>, U extends Collection<S>> Collection<S> flatMapInOut(
Collection<T> inColl, Function<T, R> mapper, Supplier<U> sup) {
return inColl.parallelStream().flatMap(mapper).collect(Collectors.toCollection(sup));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment