ListKit 一个基于Stream API的工具类

2021-08-21  本文已影响0人  sliverTwo

Stream是Java8的一大亮点,是对容器对象功能的增强,它专注于对容器对象进行各种非常便利、高效的 聚合操作(aggregate operation)或者大批量数据操作。Stream API借助于同样新出现的Lambda表达式,极大的提高编程效率和程序可读性。同时,它提供串行和并行两种模式进行汇聚操作,并发模式能够充分利用多核处理器的优势,使用fork/join并行方式来拆分任务和加速处理过程。所以说,Java8中首次出现的 java.util.stream是一个函数式语言+多核时代综合影响的产物。

距离java8发布已经过去很多年了,但是发现身边的人大部分对java8的Stream API的掌握程度还不是很高。同时,对一些我们常用的操作,使用Stream API实现还是有点繁琐,笔者当初也是花费了很多时间才将其掌握。比如根据指定key将list转换为map,或者抽取list中的某个属性。
因此,笔者编写了ListKit这个工具类,帮助大家更好的享受Stream API + lambda带来的便利,提升代码的简洁度,让代码看起来更加舒服。源码和使用demo如下,请自取。

ListKit

package com.sliver.kit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * Title: ListKit
 * Description: list工具类
 *
 * @author sliver
 * @date 2019年12月23日
 */
public final class ListKit {

    public static final int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2);

    private ListKit() {
        super();
    }

    /**
     * Title: distinct
     * Description: 过滤重复元素
     * 2020年6月24日
     *
     * @param <E>
     * @param list
     * @param keyFunction
     * @return
     */
    public static <E> List<E> distinct(List<E> list, Function<E, String> keyFunction) {
        Map<String, E> linkedMap = new LinkedHashMap<>();
        for (E item : list) {
            String key = keyFunction.apply(item);
            linkedMap.put(key, item);
        }
        return new ArrayList<>(linkedMap.values());
    }

    /**
     * Title:findFirst <br>
     * Description:获取第一个符合过滤条件的对象,不存在是返回空
     *
     * @param list      操作的list
     * @param predicate 用于判定的函数
     * @return E
     * @author sliver
     * @date 2021-08-18 20:37
     */
    public static <E> E findFirst(List<E> list, Predicate<E> predicate) {
        if (isEmpty(list)) {
            return null;
        }
        return list.stream().filter(predicate).findFirst().orElseGet(null);
    }

    /**
     * Title: findFirstRepeatElement
     * Description: 获取第一个满足过滤条件的重复元素(不包含null),若无重复数据则返回Optional.empty()
     * Date: 2020年4月14日
     *
     * @param <E>
     * @param list
     * @param operator
     * @return
     */
    public static <E> Optional<E> findFirstRepeatElement(List<E> list, Function<E, String> operator) {
        if (isEmpty(list)) {
            return Optional.empty();
        }
        HashSet<String> set = new HashSet<>(capacity(list.size()));
        for (E e : list) {
            String key = operator.apply(e);
            if (Objects.isNull(key)) {
                continue;
            }
            if (set.contains(key)) {
                return Optional.ofNullable(e);
            }
            set.add(key);
        }
        return Optional.empty();
    }

    private static <E> boolean isEmpty(List<E> list) {
        return Objects.isNull(list) || list.isEmpty();
    }


    /**
     * Title: existRepeatElement
     * Description: 判断是否存在重复的元素
     * Date: 2020年4月14日
     *
     * @param <E>
     * @param list
     * @param conditionFunction 判断条件
     * @return
     */
    public static <E> boolean existRepeatElement(List<E> list, Function<E, String> conditionFunction) {
        return findFirstRepeatElement(list, conditionFunction).isPresent();
    }

    /**
     * Title: filter
     * Description: 对List进行过滤,简化stream操作
     * Date: 2020年2月19日
     *
     * @param <E>
     * @param list
     * @param predicate 数据过滤函数
     * @return
     */
    public static <E> List<E> filter(List<E> list, Predicate<E> predicate) {
        if (isEmpty(list)) {
            return list;
        }
        return list.stream().filter(predicate).collect(Collectors.toList());
    }


    /**
     * Title:flat <br>
     * Description:list扁平化,过滤空元素
     *
     * @param list
     * @param flatFunction
     * @return java.util.List<E2>
     * @author sliver
     * @date 2021-08-18 20:52
     */
    public static <E1, E2> List<E2> flat(List<E1> list, Function<E1, List<E2>> flatFunction) {
        List<E2> resultList = new LinkedList<>();
        list.forEach(item -> {
            final List<E2> value = flatFunction.apply(item);
            if (Objects.isNull(value)) {
                return;
            }
            resultList.addAll(value);
        });
        return resultList;
    }


    /**
     * Title: convert
     * Description: 转换list对象为指定类型,跳过null
     * 2020年2月4日
     *
     * @param <E1>
     * @param <E2>
     * @param list
     * @param convertFunction
     * @return
     */
    public static <E1, E2> List<E2> convert(List<E1> list, Function<E1, E2> convertFunction) {
        return getField(list, convertFunction);
    }

    /**
     * Title: getField
     * Description: 获取指定字段,跳过null
     * Date: 2020年4月29日
     *
     * @param <T>
     * @param <F>
     * @param list
     * @param operator
     * @return
     */
    public static <T, F> List<F> getField(List<T> list, Function<T, F> operator) {
        List<F> fields = new ArrayList<>(list.size());
        list.forEach(item -> {
            final F value = operator.apply(item);
            if (Objects.isNull(value)) {
                return;
            }
            fields.add(value);
        });
        return fields;
    }

    /**
     * Title: getFieldSet
     * Description:获取指定字段集合,跳过null
     * Date: 2020年4月29日
     *
     * @param <T>
     * @param <F>
     * @param list
     * @param getFunction
     * @return
     */
    public static <T, F> Set<F> getFieldSet(List<T> list, Function<T, F> getFunction) {
        Set<F> fieldSet = new HashSet<>(capacity(list.size()));
        list.forEach(item -> {
            final F value = getFunction.apply(item);
            if (Objects.isNull(value)) {
                return;
            }
            fieldSet.add(value);
        });
        return fieldSet;
    }

    /**
     * Title: getFieldStr
     * Description: 将list中的字段使用,连接成字符串,跳过null和空字符串
     * Date: 2020年3月3日
     *
     * @param <O>
     * @param list
     * @param getFunction
     * @return
     */
    public static <O> String getFieldStr(List<O> list, Function<O, String> getFunction) {
        return getFieldStr(list, getFunction, ",");
    }

    /**
     * Title:getFieldStr <br>
     * Description:将将list中的字段使用指定分隔符连接成字符串,跳过null和空字符串
     *
     * @param list
     * @param getFunction
     * @param split       分隔符
     * @return java.lang.String
     * @author sliver
     * @date 2021-08-18 21:06
     */
    public static <O> String getFieldStr(List<O> list, Function<O, String> getFunction, String split) {
        if (Objects.isNull(split)) {
            throw new IllegalArgumentException("split cannot be null");
        }
        if (isEmpty(list)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (O item : list) {
            final String value = getFunction.apply(item);
            if (isBlank(value)) {
                continue;
            }
            sb.append(value).append(split);
        }
        if (sb.length() == 0) {
            return "";
        }
        return sb.delete(sb.length() - 1, sb.length()).toString();
    }

    private static boolean isBlank(String value) {
        return Objects.isNull(value) || Objects.equals(value, "");
    }

    /**
     * Title: convertToMap
     * Description: 转换list为map,key值跳过空字符串和null
     * Date: 2020年4月29日
     *
     * @param <O>
     * @param list
     * @param keyFunction
     * @return
     */
    public static <O> Map<String, O> convertToMap(List<O> list, Function<O, String> keyFunction) {
        if (isEmpty(list)) {
            return new HashMap<>();
        }
        Map<String, O> resultMap = new HashMap<>(capacity(list.size()));
        list.forEach(item -> {
            final String key = keyFunction.apply(item);
            if (isBlank(key)) {
                return;
            }
            resultMap.put(key, item);
        });
        return resultMap;
    }


    /**
     * Title: convertToMap
     * Description: 转换list为map,key值跳过空字符串和null
     * Date: 2020年4月29日
     *
     * @param list
     * @param keyFunction
     * @param valueFunction
     * @return
     */
    public static <K, V, E> Map<K, V> convertToMap(List<E> list, Function<E, K> keyFunction, Function<E, V> valueFunction) {
        if (isEmpty(list)) {
            return new HashMap<>();
        }
        Map<K, V> resultMap = new HashMap<>(capacity(list.size()));
        list.forEach(item -> resultMap.put(keyFunction.apply(item), valueFunction.apply(item)));
        return resultMap;
    }

    /**
     * Title: convertToMap
     * Description: 转换list为map,key值跳过空字符串和null
     * Date: 2020年4月29日
     *
     * @param list
     * @param keyFunction
     * @return
     * @see #convertToMap(List, Function, Function)
     */
    public static <E, K> Map<K, E> convert2Map(List<E> list, Function<E, K> keyFunction) {
        if (isEmpty(list)) {
            return new HashMap<>();
        }
        Map<K, E> resultMap = new HashMap<>(capacity(list.size()));
        list.forEach(item -> {
            final K key = keyFunction.apply(item);
            if (Objects.isNull(key)) {
                return;
            }
            resultMap.put(key, item);
        });
        return resultMap;
    }

    /**
     * Title: mergeToMap
     * Description: 合并相同项
     * Date: 2020年2月19日
     *
     * @param <K>           键的类型
     * @param <E>           对象的类型
     * @param list          操作的集合
     * @param keyFunction   获取key的函数
     * @param mergeFunction 合并相同key项的行数, 参数 当前项,已存在的相同项(可能为空)
     * @reEurn
     * @see #merge(List, Function, BiFunction, Function)
     */
    public static <K, E> List<E> merge(List<E> list, Function<E, K> keyFunction, BiFunction<E, E, E> mergeFunction) {
        return merge(list,keyFunction,mergeFunction,e->e);
    }


    /**
     * Title:merge <br>
     * Description:根据指定规则对list中的项进行合并
     *
     * @param <K>             键的类型
     * @param <E>             对象的类型
     * @param list            操作的集合
     * @param keyFunction     获取key的函数
     * @param mergeFunction   合并相同key项的行数, 参数 当前项,已存在的相同项(可能为空)
     * @param initValFunction 对象初始化函数
     * @return java.util.List<R>
     * @author sliver
     * @date 2021-08-21 17:22
     */
    public static <K, E, R> List<R> merge(List<E> list, Function<E, K> keyFunction, BiFunction<E, R, R> mergeFunction, Function<E, R> initValFunction) {
        Map<K, R> map = mergeToMap(list, keyFunction, mergeFunction, initValFunction);
        return new ArrayList<>(map.values());
    }


    /**
     * Title: merge2Map
     * Description: 合并相同项转换为map
     * Date: 2020年5月29日
     *
     * @param <K>
     * @param <E>
     * @param <R>
     * @param list            数据源
     * @param keyFunction     提供key的函数
     * @param mergeFunction   做合并操作的函数
     * @param initValFunction 当key对应的value不存在时,用于初始化的函数,走这个function就不会走mergeFunction
     * @return
     */
    public static <K, E, R> Map<K, R> mergeToMap(List<E> list, Function<E, K> keyFunction, BiFunction<E, R, R> mergeFunction, Function<E, R> initValFunction) {
        Map<K, R> map = new HashMap<>(capacity(list.size()));
        list.forEach(item -> {
            K key = keyFunction.apply(item);
            R target = map.get(key);
            if (Objects.isNull(target)) {
                target = initValFunction.apply(item);
                map.put(key, target);
            } else {
                map.put(key, mergeFunction.apply(item, target));
            }
        });
        return map;
    }


    /**
     * Title: groupToMap
     * Description: 根据指定规则将list聚合为Map,过滤空值
     * Date: 2020年3月18日
     *
     * @param <K>
     * @param <E>
     * @param list
     * @param keyFunction
     * @return
     */
    public static <K, E> Map<K, List<E>> groupToMap(List<E> list, Function<E, K> keyFunction) {
        return groupToMap(list, keyFunction, e -> e);
    }

    /**
     * Title: groupToMap
     * Description: 根据指定key函数和value函数将list聚合为Map,过滤空值
     * Date: 2020年3月18日
     *
     * @param <K>
     * @param <E>
     * @param list
     * @param keyFunction
     * @param valueFunction
     * @return
     */
    public static <K, E, R> Map<K, List<R>> groupToMap(List<E> list, Function<E, K> keyFunction, Function<E, R> valueFunction) {
        Map<K, List<R>> map = new HashMap<>(capacity(list.size()));
        list.forEach(item -> {
            K key = keyFunction.apply(item);
            List<R> target = map.get(key);
            if (Objects.isNull(target)) {
                target = new ArrayList<>();
                map.put(key, target);
            }
            target.add(valueFunction.apply(item));
        });
        return map;
    }


    /**
     * Title:setList <br>
     * Description:据关联字段进行1对多设置值,
     *
     * @param list1
     * @param key1Function
     * @param list2
     * @param key2Function
     * @param setConsumer
     * @return void
     * @author sliver
     * @date 2020-11-19 20:09
     */
    public static <E1, E2> void setList(List<E1> list1, Function<E1, String> key1Function, List<E2> list2, Function<E2, String> key2Function, BiConsumer<E1, List<E2>> setConsumer) {
        if (isEmpty(list1) || isEmpty(list2)) {
            return;
        }
        Map<String, List<E2>> list2Map = groupToMap(list2, key2Function);
        for (E1 e1 : list1) {
            String key = key1Function.apply(e1);
            List<E2> l2 = list2Map.get(key);
            if (!isEmpty(l2)) {
                setConsumer.accept(e1, l2);
            }
        }
    }

    /**
     * Title:set <br>
     * Description:根据关联字段进行1对1或者一对多设置值。通常用于一对多或者一对一的表的连接
     *
     * @param list1
     * @param key1Function 获取list1中与list2的关联字段的函数
     * @param list2
     * @param key2Function 获取list2中与list1的关联字段的函数
     * @param setConsumer  设置值的函数
     * @return void
     * @author sliver
     * @date 2020-11-19 20:09
     */
    public static <E1, E2> void set(List<E1> list1, Function<E1, String> key1Function, List<E2> list2, Function<E2, String> key2Function, BiConsumer<E1, E2> setConsumer) {
        if (isEmpty(list1) || isEmpty(list2)) {
            return;
        }
        Map<String, E2> list2Map = convert2Map(list2, key2Function);
        for (E1 e1 : list1) {
            String key = key1Function.apply(e1);
            E2 e2 = list2Map.get(key);
            if (Objects.nonNull(e2)) {
                setConsumer.accept(e1, e2);
            }
        }
    }

    /**
     * Returns a capacity that is sufficient to keep the map from being resized as
     * long as it grows no larger than expectedSize and the load factor is >= its
     * default (0.75).
     */
    static int capacity(int expectedSize) {
        if (expectedSize < MAX_POWER_OF_TWO) {
            return expectedSize + expectedSize / 3;
        }
        // any large value
        return Integer.MAX_VALUE;
    }

}

使用示例 ListKitDemo

package com.sliver.kit;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

/**
 * <p>Title: ListKitDemo</p>
 * <p>Description: ListKit使用实例</p>
 * 我们使用学生对象和成绩表对象进行演示,它们的关系是一对多的关系,
 * 以下demo根据使用频次进行排序
 *
 * @author sliver
 * @email 18142611739@163.com
 * @date 2021-08-21 16:43
 */
public class ListKitDemo {
    private static List<Student> studentList;
    private static List<Grade> gradeList;

    static void initData(){
        // 数据准备
        studentList = new ArrayList<>();
        studentList.add(new Student("001", "张三"));
        studentList.add(new Student("002", "李四"));
        studentList.add(new Student("003", "王五"));
        gradeList = new ArrayList<>();
        gradeList.add(new Grade("001", "数学", 88));
        gradeList.add(new Grade("001", "语文", 78));
        gradeList.add(new Grade("001", "英语", 90));
        gradeList.add(new Grade("002", "数学", 83));
        gradeList.add(new Grade("002", "语文", 73));
        gradeList.add(new Grade("002", "英语", 93));
        gradeList.add(new Grade("003", "数学", 85));
        gradeList.add(new Grade("003", "语文", 75));
        gradeList.add(new Grade("003", "英语", 95));
    }

    public static void main(String[] args) {
        System.out.println("=======testConvert=======");
        testConvert();
        System.out.println("\n=======testGroup=======");
        testGroup();
        System.out.println("\n=======testMerge=======");
        testMerge();
        System.out.println("\n=======testSet=======");
        testSet();
        System.out.println("\n=======testOther=======");
        testOther();
    }

    /**
     * Title:testConvert <br>
     * Description:演示convert、convertToMap的用法
     *
     * @return void
     * @date 2021-08-21 17:33
     */
    static void testConvert() {
        initData();
        // 使用ListKit.convert从students中获取学生姓名数组,也可以用ListKit.getField,与convert的实现一致,在此场景下更符合语义
        List<String> nameList = ListKit.convert(studentList, Student::getCode);
        System.out.println("ListKit.convert(studentList, Student::getCode):");
        System.out.println(nameList);
        // 从gradeList中获取有分数的学生编号
        Set<String> studentCodeSet = ListKit.getFieldSet(gradeList, Grade::getStudentCode);
        System.out.println("ListKit.getFieldSet(gradeList, Grade::getStudentCode):");
        System.out.println(studentCodeSet);
        // 将studentList转换为map,使用code作为key
        Map<String, Student> studentMap = ListKit.convert2Map(studentList, Student::getCode);
        System.out.println("ListKit.convert2Map(studentList, Student::getCode):");
        System.out.println(studentMap);
        // 将gradeList转换为map,使用studentCode+course作为key
        Map<String, Grade> gradeMap = ListKit.convert2Map(gradeList, e -> e.getStudentCode() + e.getCourse());
        System.out.println("ListKit.convert2Map(gradeList, e -> e.getStudentCode() + e.getCourse()):");
        System.out.println(gradeMap);
    }

    /**
     * Title:testGroup <br>
     * Description:测试分组相关方法
     *
     * @return void
     * @date 2021-08-21 17:42
     */
    static void testGroup() {
        initData();
        // 1. 将gradeList按studentCode分组
        Map<String, List<Grade>> gradeListMap = ListKit.groupToMap(gradeList, Grade::getStudentCode);
        System.out.println("ListKit.groupToMap(gradeList, Grade::getStudentCode):");
        System.out.println(gradeListMap);
        // 2. 将gradeList按studentCode分组,并且将value转换为score
        Map<String, List<Integer>> gradeListMap2 = ListKit.groupToMap(gradeList, Grade::getStudentCode, Grade::getScore);
        System.out.println("ListKit.groupToMap(gradeList, Grade::getStudentCode, Grade::getScore):");
        System.out.println(gradeListMap2);
    }

    /**
     * Title:testMerge <br>
     * Description:测试合并方法
     *
     * @return void
     * @date 2021-08-21 17:51
     */
    static void testMerge() {
        initData();
        // 1. 计算每个学生的总分
        List<Integer> gradeList = ListKit.merge(ListKitDemo.gradeList, Grade::getStudentCode, (item, target) -> target + item.getScore(), e -> e.getScore());
        System.out.println("ListKit.merge(gradeList, Grade::getStudentCode, (item, target) -> target+ item.getScore(), e -> e.getScore())");
        System.out.println(gradeList);
        Map<String, Integer> gradeMap = ListKit.mergeToMap(ListKitDemo.gradeList, Grade::getStudentCode, (item, target) -> target + item.getScore(), e -> e.getScore());
        System.out.println("ListKit.mergeToMap(ListKitDemo.gradeList, Grade::getStudentCode, (item, target) -> target + item.getScore(), e -> e.getScore())");
        System.out.println(gradeMap);
        // 2. 将gradeList按studentCode分组,并且将value转换为score
        Map<String, List<Integer>> gradeListMap2 = ListKit.groupToMap(ListKitDemo.gradeList, Grade::getStudentCode, Grade::getScore);
        System.out.println("ListKit.groupToMap(gradeList, Grade::getStudentCode, Grade::getScore):");
        System.out.println(gradeListMap2);
    }



    /**
     * Title:testSet <br>
     * Description:测试set方法,复合操作
     *
     * @return void
     * @date 2021-08-21 18:04
     */
    static void testSet() {
        initData();
        // 将gradeList放入studentList中
        ListKit.setList(studentList, Student::getCode, gradeList, Grade::getStudentCode, (student, gradeList) -> student.setScoreList(gradeList));
        System.out.println("ListKit.setList(studentList,Student::getCode,gradeList,Grade::getStudentCode,(student, gradeList)->student.setScoreList(gradeList))");
        System.out.println(studentList);
        // 设置总分数
        List<Grade> gradeList = ListKit.merge(ListKitDemo.gradeList, Grade::getStudentCode, (item, target) -> target.setScore(target.getScore() + item.getScore()));
        ListKit.set(studentList, Student::getCode, gradeList, Grade::getStudentCode, (student, grade) -> student.setTotalScore(grade.getScore()));
        System.out.println("ListKit.set(studentList, Student::getCode, gradeList, Grade::getStudentCode, (student, grade) -> student.setTotalScore(grade.getScore()))");
        System.out.println(studentList);
    }

    /**
     * Title:testOther <br>
     * Description:测试其它方法
     *
     * @return void
     * @date 2021-08-21 18:14
     */
    static void testOther() {
        initData();
        studentList.add(new Student("002", "李四"));
        // 1. 获取张三的信息
        Student student = ListKit.findFirst(studentList, e -> Objects.equals(e.getName(), "张三"));
        System.out.println("ListKit.findFirst(studentList, e -> Objects.equals(e.getName(), \"张三\"))");
        System.out.println(student);
        // 2. 获取重复信息
        // 判断是否存在重复信息
        boolean existRepeatElement = ListKit.existRepeatElement(studentList, Student::getCode);
        System.out.println("ListKit.existRepeatElement(studentList, Student::getCode)");
        System.out.println(existRepeatElement);
        Optional<Student> optional = ListKit.findFirstRepeatElement(studentList, Student::getCode);
        System.out.println("ListKit.findFirstRepeatElement(studentList,Student::getCode)");
        optional.ifPresent(e -> System.out.println(e));
        // 3. 排除重复信息
        List<Student> studentList2 = ListKit.distinct(studentList, Student::getCode);
        System.out.println("ListKit.distinct(studentList2, Student::getCode)");
        System.out.println(studentList2);
        // 4. 获取分数大于80的信息
        List<Grade> gradeList2 = ListKit.filter(ListKitDemo.gradeList, grade -> grade.getScore() > 80);
        System.out.println("ListKit.filter(ListKitDemo.gradeList, grade -> grade.getScore() > 80)");
        System.out.println(gradeList2);
        // 5. 将list中的list拉平
        ListKit.setList(studentList, Student::getCode, gradeList, Grade::getStudentCode, (stu, gradeList) -> stu.setScoreList(gradeList));
        List<Grade> gradeList1 = ListKit.flat(studentList, Student::getScoreList);
        System.out.println(" ListKit.flat(studentList, Student::getScoreList)");
        System.out.println(gradeList1);
    }
}

class Student {
    /**
     * 学号
     */
    private String code;

    /**
     * 姓名
     */
    private String name;
    private Integer totalScore;

    private List<Grade> scoreList;

    public Student(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public Integer getTotalScore() {
        return totalScore;
    }

    public Student setTotalScore(Integer totalScore) {
        this.totalScore = totalScore;
        return this;
    }

    public String getCode() {
        return code;
    }

    public Student setCode(String code) {
        this.code = code;
        return this;
    }

    public String getName() {
        return name;
    }

    public Student setName(String name) {
        this.name = name;
        return this;
    }

    public List<Grade> getScoreList() {
        return scoreList;
    }

    public Student setScoreList(List<Grade> scoreList) {
        this.scoreList = scoreList;
        return this;
    }

    @Override
    public String toString() {
        return "Student{" +
                "code='" + code + '\'' +
                ", name='" + name + '\'' +
                ", scoreList=" + scoreList +
                '}';
    }
}

class Grade {
    /**
     * 学生编号
     */
    private String studentCode;
    /**
     * 课程
     */
    private String course;
    /**
     * 分数
     */
    private Integer score;

    public Grade(String studentCode, String course, Integer score) {
        this.studentCode = studentCode;
        this.course = course;
        this.score = score;
    }

    public String getStudentCode() {
        return studentCode;
    }

    public Grade setStudentCode(String studentCode) {
        this.studentCode = studentCode;
        return this;
    }

    public String getCourse() {
        return course;
    }

    public Grade setCourse(String course) {
        this.course = course;
        return this;
    }

    public Integer getScore() {
        return score;
    }

    public Grade setScore(Integer score) {
        this.score = score;
        return this;
    }

    @Override
    public String toString() {
        return "Grade{" +
                "studentCode='" + studentCode + '\'' +
                ", course='" + course + '\'' +
                ", score='" + score + '\'' +
                '}';
    }
}

demo方法运行结果

=======testConvert=======
ListKit.convert(studentList, Student::getCode):
[001, 002, 003]
ListKit.getFieldSet(gradeList, Grade::getStudentCode):
[001, 002, 003]
ListKit.convert2Map(studentList, Student::getCode):
{001=Student{code='001', name='张三', scoreList=null}, 002=Student{code='002', name='李四', scoreList=null}, 003=Student{code='003', name='王五', scoreList=null}}
ListKit.convert2Map(gradeList, e -> e.getStudentCode() + e.getCourse()):
{001数学=Grade{studentCode='001', course='数学', score='88'}, 002语文=Grade{studentCode='002', course='语文', score='73'}, 003语文=Grade{studentCode='003', course='语文', score='75'}, 003英语=Grade{studentCode='003', course='英语', score='95'}, 002英语=Grade{studentCode='002', course='英语', score='93'}, 001英语=Grade{studentCode='001', course='英语', score='90'}, 001语文=Grade{studentCode='001', course='语文', score='78'}, 003数学=Grade{studentCode='003', course='数学', score='85'}, 002数学=Grade{studentCode='002', course='数学', score='83'}}

=======testGroup=======
ListKit.groupToMap(gradeList, Grade::getStudentCode):
{001=[Grade{studentCode='001', course='数学', score='88'}, Grade{studentCode='001', course='语文', score='78'}, Grade{studentCode='001', course='英语', score='90'}], 002=[Grade{studentCode='002', course='数学', score='83'}, Grade{studentCode='002', course='语文', score='73'}, Grade{studentCode='002', course='英语', score='93'}], 003=[Grade{studentCode='003', course='数学', score='85'}, Grade{studentCode='003', course='语文', score='75'}, Grade{studentCode='003', course='英语', score='95'}]}
ListKit.groupToMap(gradeList, Grade::getStudentCode, Grade::getScore):
{001=[88, 78, 90], 002=[83, 73, 93], 003=[85, 75, 95]}

=======testMerge=======
ListKit.merge(gradeList, Grade::getStudentCode, (item, target) -> target+ item.getScore(), e -> e.getScore())
[256, 249, 255]
ListKit.mergeToMap(ListKitDemo.gradeList, Grade::getStudentCode, (item, target) -> target + item.getScore(), e -> e.getScore())
{001=256, 002=249, 003=255}
ListKit.groupToMap(gradeList, Grade::getStudentCode, Grade::getScore):
{001=[88, 78, 90], 002=[83, 73, 93], 003=[85, 75, 95]}

=======testSet=======
ListKit.setList(studentList,Student::getCode,gradeList,Grade::getStudentCode,(student, gradeList)->student.setScoreList(gradeList))
[Student{code='001', name='张三', scoreList=[Grade{studentCode='001', course='数学', score='88'}, Grade{studentCode='001', course='语文', score='78'}, Grade{studentCode='001', course='英语', score='90'}]}, Student{code='002', name='李四', scoreList=[Grade{studentCode='002', course='数学', score='83'}, Grade{studentCode='002', course='语文', score='73'}, Grade{studentCode='002', course='英语', score='93'}]}, Student{code='003', name='王五', scoreList=[Grade{studentCode='003', course='数学', score='85'}, Grade{studentCode='003', course='语文', score='75'}, Grade{studentCode='003', course='英语', score='95'}]}]
ListKit.set(studentList, Student::getCode, gradeList, Grade::getStudentCode, (student, grade) -> student.setTotalScore(grade.getScore()))
[Student{code='001', name='张三', scoreList=[Grade{studentCode='001', course='数学', score='256'}, Grade{studentCode='001', course='语文', score='78'}, Grade{studentCode='001', course='英语', score='90'}]}, Student{code='002', name='李四', scoreList=[Grade{studentCode='002', course='数学', score='249'}, Grade{studentCode='002', course='语文', score='73'}, Grade{studentCode='002', course='英语', score='93'}]}, Student{code='003', name='王五', scoreList=[Grade{studentCode='003', course='数学', score='255'}, Grade{studentCode='003', course='语文', score='75'}, Grade{studentCode='003', course='英语', score='95'}]}]

=======testOther=======
ListKit.findFirst(studentList, e -> Objects.equals(e.getName(), "张三"))
Student{code='001', name='张三', scoreList=null}
ListKit.existRepeatElement(studentList, Student::getCode)
true
ListKit.findFirstRepeatElement(studentList,Student::getCode)
Student{code='002', name='李四', scoreList=null}
ListKit.distinct(studentList2, Student::getCode)
[Student{code='001', name='张三', scoreList=null}, Student{code='002', name='李四', scoreList=null}, Student{code='003', name='王五', scoreList=null}]
ListKit.filter(ListKitDemo.gradeList, grade -> grade.getScore() > 80)
[Grade{studentCode='001', course='数学', score='88'}, Grade{studentCode='001', course='英语', score='90'}, Grade{studentCode='002', course='数学', score='83'}, Grade{studentCode='002', course='英语', score='93'}, Grade{studentCode='003', course='数学', score='85'}, Grade{studentCode='003', course='英语', score='95'}]
ListKit.setList(studentList, Student::getCode, gradeList, Grade::getStudentCode, (student, gradeList) -> student.setScoreList(gradeList))
[Grade{studentCode='001', course='数学', score='88'}, Grade{studentCode='001', course='语文', score='78'}, Grade{studentCode='001', course='英语', score='90'}, Grade{studentCode='002', course='数学', score='83'}, Grade{studentCode='002', course='语文', score='73'}, Grade{studentCode='002', course='英语', score='93'}, Grade{studentCode='003', course='数学', score='85'}, Grade{studentCode='003', course='语文', score='75'}, Grade{studentCode='003', course='英语', score='95'}, Grade{studentCode='002', course='数学', score='83'}, Grade{studentCode='002', course='语文', score='73'}, Grade{studentCode='002', course='英语', score='93'}]
上一篇下一篇

猜你喜欢

热点阅读