import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.slf4j.Logger;
public class LambdaUtil {
private static final Logger logger = LoggerFactory.getLogger();
public LambdaUtil() {
}
public static void runWithTry(LambdaUtil.Runnable r) {
try {
r.run();
} catch (Exception var2) {
logger.error("run throws exception.", var2);
}
}
public static LambdaUtil.ExceptionEntity runWithCatch(LambdaUtil.Runnable r) {
try {
r.run();
} catch (Exception var2) {
return new LambdaUtil.ExceptionEntity(var2);
}
return new LambdaUtil.ExceptionEntity((Exception)null);
}
public static <K, E> Map<K, List<E>> groupByKey(List<E> elements, Function<E, K> getKey) {
Map<K, List<E>> result = new HashMap(elements.size() * 4 / 3 + 1);
Iterator var3 = elements.iterator();
while(var3.hasNext()) {
E element = var3.next();
K key = getKey.apply(element);
List<E> list = (List)result.computeIfAbsent(key, (k) -> {
return new ArrayList();
});
list.add(element);
}
return result;
}
public static <K, E, R> Map<K, R> mapConversion(Map<K, E> rawMap, Function<E, R> f) {
return (Map)rawMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey, (it) -> {
return f.apply(it.getValue());
}));
}
public static <K, E, R> Map<K, R> mapConversion(Map<K, E> rawMap, BiFunction<K, E, R> f) {
return (Map)rawMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey, (it) -> {
return f.apply(it.getKey(), it.getValue());
}));
}
public static <T> List<T> filter(Collection<T> collection, Predicate<? super T> predicate) {
return (List)collection.stream().filter(predicate).collect(Collectors.toList());
}
@FunctionalInterface
public interface Runnable {
void run() throws Exception;
}
public static class ExceptionEntity {
private Exception e;
private ExceptionEntity(Exception e) {
this.e = e;
}
public void handle(LambdaUtil.ExceptionEntity.IException iException) {
if (this.e != null) {
iException.handle(this.e);
}
}
public interface IException {
void handle(Exception e);
}
}
}
LambdaUtil,Lambda方式运行你的接口
最新推荐文章于 2024-09-22 10:46:49 发布