package net.northking.cctp.dw.utils;

import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: puchao.ma
 * @date: 2021/10/15 16:38
 */
public class ListUtils {

    /**
     * 以groupCondition为分组条件，返回map
     * key为id, value为list
     * List.groupBy(BugManagement, BugManagement::bugId)
     * @param list
     * @param groupCondition
     * @param <T>
     * @param <K>
     * @return
     */
    public static <T,K> Map<? extends K, List<T>> groupBy(List<T> list, Function<? super T, ? extends K> groupCondition) {

        if (CollectionUtils.isEmpty(list)) {
            return new HashMap<>();
        }

        return list.stream().collect(Collectors.groupingBy(groupCondition));
    }

}
