com.github.andyshao.util.stream.StreamOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Gear Show documentation
Show all versions of Gear Show documentation
Enhance and formating the coding of JDK
The newest version!
package com.github.andyshao.util.stream;
import com.github.andyshao.util.CollectionOperation;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.BaseStream;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
*
*
* Title:
* Descript:
* Copyright: Copryright(c) May 27, 2019
* Encoding: UNIX UTF-8
*
* @author Andy.Shao
*
*/
public final class StreamOperation {
/**
* all of matched
* @param stream {@link Stream}
* @param predicate {@link Predicate}
* @return {@link Pair}
* @param data type
*/
public static Pair, Boolean> allOfMatched(Stream stream, Predicate super T> predicate) {
List ls = stream.filter(predicate)
.collect(Collectors.toList());
return CollectionOperation.isEmptyOrNull(ls) ? Pair.of(ls, false) : Pair.of(ls, true);
}
/**
* any match
* @param stream {@link Stream}
* @param predicate {@link Predicate}
* @return {@link Pair}
* @param data type
*/
public static Pair anyMatch(Stream stream, Predicate super T> predicate) {
Optional op = stream.filter(predicate)
.findAny();
return op.isPresent() ? Pair.of(op.get(), true) : Pair.of(null, false);
}
/**
* distinct
* @param stream {@link Stream}
* @param keyExtractor key extractor
* @return {@link Stream}
* @param data type
*/
public static Stream distinct(Stream stream, Function super T, ?> keyExtractor) {
return stream.filter(distinctByKey(keyExtractor));
}
/**
* distinct by key
* @param keyExtractor key extractor
* @return {@link Predicate}
* @param data type
*/
public static Predicate distinctByKey(Function super T, ?> keyExtractor) {
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy