LambdaUtil,Lambda方式运行你的接口


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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值