java lambda之ListHelper

import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * lambda之ListHelper
 * Created by zhaojiy on 2018/12/15.
 */
public class ListHelper {
    //example ListHelper.firstOrDefault(students, n -> n.getAge()==12)
    public static <T> T firstOrDefault(List<T> list, Predicate<? super T> predicate) {
        if (list != null) {
            Optional<T> first = list.stream().filter(predicate).findFirst();
            if (first.isPresent()) {
                return first.get();
            }
        }
        return null;
    }

    //example ListHelper.where(students, n -> n.getAge() >12)
    public static <T> List<T> where(List<T> list, Predicate<? super T> predicate) {
        if (list != null) {
            return list.stream().filter(predicate).collect(Collectors.toList());
        }
        return null;
    }

    //example ListHelper.select(students, n -> n.getAge())
    public static <T, E> List<E> select(List<T> list, Function<? super T, E> express) {
        List<E> eList = new ArrayList<>();
        for (T t : list) {
            E e = express.apply(t);
            eList.add(e);
        }
        return eList;
    }

    //是否重复
    // example ListHelper.isRepeated(students,n->n.getName()
    public static <T, E> boolean isRepeated(List<T> list, Function<? super T, E> express) {
        for (int i = 0; i < list.size()-1; i++) {
            E e = express.apply(list.get(i));
            for (int j = i + 1; j <list.size(); j++) {
                E e2 = express.apply(list.get(j));
                if (e.equals(e2)) {
                    return true;
                }
            }
        }
        return false;
    }


    //分组
    public  static  <T, E>  HashMap<E,List<T>> group(List<T> list, Function<? super T, E> express) {
        HashMap<E,List<T>>  groups=new HashMap<>();
        for (T t:list){
            E e = express.apply(t);
            if(groups.keySet().contains(e)){
                groups.get(e).add(t);
            }else
            {
                List<T> tempList=new ArrayList<>();
                tempList.add(t);
                groups.put(e,tempList);
            }
        }
        return groups;
    }

    public static <K, V> Map<K, V> listToMap(List<V> list, String keyMethodName, Class<V> c) {
        Map<K, V> map = new HashMap<K, V>();
        if (list != null) {
            try {
                Method methodGetKey = c.getMethod(keyMethodName);
                for (int i = 0; i < list.size(); i++) {
                    V value = list.get(i);
                    @SuppressWarnings("unchecked")
                    K key = (K) methodGetKey.invoke(list.get(i));
                    map.put(key, value);
                }
            } catch (Exception e) {
                throw new IllegalArgumentException("field can't match the key!");
            }
        }

        return map;
    }


    // example ListHelper.orderBy(students,n->n.getAge())
    public static <T, U extends Comparable<? super U>> List<T> orderBy(List<T> list, Function<? super T, ? extends U> keyExtractor) {
        list.sort(Comparator.comparing(keyExtractor));
        return list;
    }


    // example ListHelper.orderBy(students,n->n.getAge())
    public static <T, U extends Comparable<? super U>> List<T> orderByDesc(List<T> list, Function<? super T, ? extends U> keyExtractor) {
        list.sort(Comparator.comparing(keyExtractor).reversed());
        return list;
    }

    // example  ListHelper.max(students,n->n.getAge())
    public static <T, U extends Comparable<? super U>> T max(List<T> list, Function<? super T, ? extends U> keyExtractor) {
        list.sort(Comparator.comparing(keyExtractor));
        if (list.size() > 0) {
            return list.get(list.size() - 1);
        }
        return null;
    }

    // example  ListHelper.min(student,n->n.getAge())
    public static <T, U extends Comparable<? super U>> T min(List<T> list, Function<? super T, ? extends U> keyExtractor) {
        list.sort(Comparator.comparing(keyExtractor));
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值