集合分组工具类

自定义分组

/**
 * 集合分组工具类
 *
 * @autor WYH
 */
public class GroupUtils {

    /**
     * 集合分组算法封装。
     *
     * @param colls 待分组的集合
     * @param gb    分组依据
     * @return Map<T, List < E>>
     */
    public static final <T extends Comparable<T>, E> Map<T, List<E>> group(Collection<E> colls, GroupBy<T> gb) {

        if (colls == null || colls.isEmpty()) { // 分组集合为空
            return null;
        }
        if (gb == null) {// 分组依据为空
            return null;
        }
        Map<T, List<E>> map = new HashMap<T, List<E>>();
        Iterator<E> it = colls.iterator();
        while (it.hasNext()) {
            E e = it.next();
            T t = gb.groupby(e);
            if (map.containsKey(t)) {
                map.get(t).add(e);
            } else {
                List<E> list = new ArrayList<E>();
                list.add(e);
                map.put(t, list);
            }
        }
        return map;
    }

    /**
     * 返回空数据
     *
     * @param colls
     * @param gb
     * @param <T>
     * @param <E>
     * @return
     */
    public static <T extends Comparable<T>, E> Map<T, List<E>> groupNew(Collection<E> colls, GroupBy<T> gb) {

        if (colls == null || colls.isEmpty()) { // 分组集合为空
            return new HashMap<>();
        }
        if (gb == null) {// 分组依据为空
            return new HashMap<>();
        }
        Map<T, List<E>> map = new HashMap<T, List<E>>();
        Iterator<E> it = colls.iterator();
        while (it.hasNext()) {
            E e = it.next();
            T t = gb.groupby(e);
            if (map.containsKey(t)) {
                map.get(t).add(e);
            } else {
                List<E> list = new ArrayList<E>();
                list.add(e);
                map.put(t, list);
            }
        }
        return map;
    }
}

/**
 * 分组依据接口
 *
 * @autor WYH
 * @since 2019-08-22
 */
public interface GroupBy<T> {

    T groupby(Object obj);
}

测试

public class GroupTest {

    public static void main(String[] args) {

        Map<String, String> map = new HashMap<>();
        map.put("name","张三");
        map.put("age","20");
        map.put("dept","开发");
        Map<String, String> map1 = new HashMap<>();
        map1.put("name","李四");
        map1.put("age","21");
        map1.put("dept","人事");
        Map<String, String> map2 = new HashMap<>();
        map2.put("name","王五");
        map2.put("age","22");
        map2.put("dept","行政");
        Map<String, String> map3 = new HashMap<>();
        map3.put("name","米卡");
        map3.put("age","22");
        map3.put("dept","人事");

        List<Map<String, String>> list = new ArrayList<>();
        list.add(map);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        // 将数据分组
        Map<String, List<Map<String, String>>> group = GroupUtils.group(list, new GroupBy<String>() {
            @Override
            public String groupby(Object obj) {
                if (obj instanceof Map) {
                    Map<String, String> map = (Map<String, String>) obj;
                    return (String) map.get("age");
                }
                return null;
            }
        });
        System.out.println(group);
    }
}

打印结果:

{22=[{name=王五, dept=行政, age=22}, {name=米卡, dept=人事, age=22}], 20=[{name=张三, dept=开发, age=20}], 21=[{name=李四, dept=人事, age=21}]}

Java8

//按照多个属性分组,key为多个属性合
Map<String, List<Map<String, String>>> collect = list.stream().collect(Collectors.groupingBy(e -> e.get("dept") + e.get("age")));
System.out.println(collect);
//按照多个属性分组,key为单个属性合
Map<String, Map<String, List<Map<String, String>>>> result = list.stream().collect(Collectors.groupingBy(e -> e.get("dept"), Collectors.groupingBy(e -> e.get("age"))));
System.out.println(result);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汪了个王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值