Skip to content

Instantly share code, notes, and snippets.

@iamsonnn
Last active November 29, 2023 13:12
Show Gist options
  • Save iamsonnn/8fecc273dae84650184a43909de4f2c2 to your computer and use it in GitHub Desktop.
Save iamsonnn/8fecc273dae84650184a43909de4f2c2 to your computer and use it in GitHub Desktop.
Stream Utils for handling common collection processing use cases
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class StreamUtils {
public static <S, D> List<D> mapToList(Collection<S> sourceList, Function<S, D> mapFunction){
if (sourceList == null || sourceList.isEmpty()) return Collections.emptyList();
return sourceList
.stream()
.map(mapFunction)
.collect(Collectors.toList());
}
public static <S, D> Set<D> mapToSet(Collection<S> sourceList, Function<S, D> mapFunction){
if (sourceList == null || sourceList.isEmpty()) return Collections.emptySet();
return sourceList
.stream()
.map(mapFunction)
.collect(Collectors.toSet());
}
public static <S, D> List<D> mapToList(S[] sourceList, Function<S, D> mapFunction){
if (sourceList == null) return Collections.emptyList();
return Arrays.asList(sourceList)
.stream()
.map(mapFunction)
.collect(Collectors.toList());
}
public static <S> Set<S> filterToSet(Collection<S> sourceList, Predicate<S> condition){
if (sourceList == null || sourceList.isEmpty()) return Collections.emptySet();
return sourceList
.stream()
.filter(condition)
.collect(Collectors.toSet());
}
// return null if not found or empty
public static <S> S first(Collection<S> sourceList){
if (sourceList == null || sourceList.isEmpty()) return null;
return sourceList
.stream()
.findFirst()
.orElse(null);
}
// return null if not found or empty
public static <S> S first(S[] sourceList){
if (sourceList == null || sourceList.length == 0) return null;
return Arrays.stream(sourceList)
.findFirst()
.orElse(null);
}
// return null if not found or empty
public static <S> S firstMatch(Collection<S> sourceList, Predicate<S> condition){
if (sourceList == null || sourceList.isEmpty()) return null;
return sourceList
.stream()
.filter(condition)
.findFirst()
.orElse(null);
}
// return null if not found or empty
public static <S> S firstMatch(S[] sourceList, Predicate<S> condition){
if (sourceList == null || sourceList.length == 0) return null;
return Arrays.stream(sourceList)
.filter(condition)
.findFirst()
.orElse(null);
}
public static <S> List<S> filterToList(Collection<S> sourceList, Predicate<S> condition) {
if (sourceList == null || sourceList.isEmpty()) return Collections.emptyList();
return sourceList
.stream()
.filter(condition)
.collect(Collectors.toList());
}
public static <I, O> Map<I, O> toIdMap(Collection<O> sourceList, Function<O, I> idFunction) {
Map<I, O> map = new HashMap<>();
if (sourceList == null || sourceList.isEmpty()) return map;
sourceList.forEach(element -> map.put(idFunction.apply(element), element));
return map;
}
public static <I, O> Set<O> getMapValueWithIds(Map<I, O> map, List<I> ids) {
if (map == null || map.isEmpty()) return new HashSet<>();
return ids.stream().map(i -> map.getOrDefault(i, null)).filter(Objects::nonNull).collect(Collectors.toSet());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment