分组统计多个类型字段

import cn.hutool.core.util.StrUtil;
import com.luoan.vulnmanager.config.enums.VulnLevelEnum;
import com.luoan.vulnmanager.domain.ScanExcel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 分组统计多个类型字段
 *
 * @author jason
 */
public class GroupSum {

    public static void main(String[] args) {
        List<ScanExcel> valueList = new ArrayList<>();
        valueList.add(new ScanExcel().setVulnerabilityLevel("紧急"));
        valueList.add(new ScanExcel().setVulnerabilityLevel("高危"));
        valueList.add(new ScanExcel().setVulnerabilityLevel("高危"));
        valueList.add(new ScanExcel().setVulnerabilityLevel("中危"));

        Map<String, Integer> sumMap = valueList.stream()
                .collect(Collectors.groupingBy(ScanExcel::getVulnerabilityLevel,
                        Collectors.summingInt(scan -> {
                            if (StrUtil.equals(VulnLevelEnum.EMERGENCY.getName(), scan.getVulnerabilityLevel())) {
                                return 1;
                            }
                            if (StrUtil.equals(VulnLevelEnum.HIGH.getName(), scan.getVulnerabilityLevel())) {
                                return 1;
                            }
                            if (StrUtil.equals(VulnLevelEnum.MEDIUM.getName(), scan.getVulnerabilityLevel())) {
                                return 1;
                            }
                            return 0;
                        })));

        System.out.println(sumMap);

        /**
         * {紧急=1, 高危=2, 中危=1}
         */
    }

}

你可能感兴趣的:(干货,java,jvm,开发语言,group,by,stream)