Java Utililty Methods Stream Operation

List of utility methods to do Stream Operation

Description

The list of methods to do Stream Operation are organized into topic(s).

Method

StreamfindStreamAmongst(Class clazz, Collection instances)
find Stream Amongst
return instances.stream().filter(clazz::isInstance).map(clazz::cast);
TfirstValue(Stream stream)
first Value
return stream.findAny().get();
StreamflatOptionals(Stream> list)
flat Optionals
return list.filter(Optional::isPresent).map(opt -> opt.get());
Map>flattenFeatureStreamToMap( Stream>>> stream)
Convert a flatten nested stream(feature family, stream(feature name.
Map<String, Map<String, Double>> outputFeatureMap = new HashMap<>();
stream.forEach(inputFamilyEntry -> {
    String familyName = inputFamilyEntry.getKey();
    Map<String, Double> outputFeatureFamily = outputFeatureMap.get(familyName);
    if (outputFeatureFamily == null) {
        outputFeatureFamily = new HashMap<>();
        outputFeatureMap.put(familyName, outputFeatureFamily);
    final Map<String, Double> finalFeatures = outputFeatureFamily;
    inputFamilyEntry.getValue().forEach(feature -> finalFeatures.put(feature.getKey(), feature.getValue()));
});
return outputFeatureMap;
intgcd(IntStream numbers)
Calculate the greatest commom divisor of all numbers in the int stream
return numbers.reduce(0, (x, y) -> gcd(x, y));
IntStreamgetOSIllegalCharacterStream(String path)
get OS Illegal Character Stream
return path.chars().filter(c -> c < '\u0020' || WINDOWS_RESERVED_CHARS.indexOf(c) != -1
        || reserverdCharacters.indexOf(c) != -1);
StreamgetStream(Iterable iterable)
Get a sequential Stream from an Iterable .
return StreamSupport.stream(iterable.spliterator(), false);
StreamgetStringStreamFromArray(String... ids)
Returns a stream of strings from a string array
return Stream.of(ids);
LongStreaminfiniteParallelStream()
Creates an infinite parallel long stream.
return iterate(1L, l -> l + 1).parallel();
Streaminterleave(Stream a, Stream b)
interleave
List<T9> ar = a.collect(Collectors.toList());
List<T9> br = b.collect(Collectors.toList());
ArrayList<T9> r = new ArrayList<>(ar.size() + br.size());
for (int i = 0; i < Math.max(ar.size(), br.size()); i++) {
    if (i < ar.size())
        r.add(ar.get(i));
    if (i < br.size())
        r.add(br.get(i));
...