Java实现多字段分组计数

一 需求

以产品类型和国家两个字段为关键字,进行计数。比如中国的电脑有多少种,韩国的洗衣机有多少种?

产品类型

国家

分类

电脑

中国

台式

洗衣机

韩国

滚筒

电脑

中国

笔记本

洗衣机

韩国

一般

电脑

中国

平板

电脑

韩国

笔记本

洗衣机

中国

滚筒

电脑

韩国

平板

洗衣机

中国

一般

二 代码

package com.cakin.javademo;

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

/**
* @ClassName: GroupCount
* @Description: 分组计数 参考:http://www.cielni.com/2018/07/14/java-stream-groupingby/
* @Date: 2021/3/13
* @Author: cakin
*/
public class GroupCount {
    /**
     * 功能描述:分组计数
     *
     * @author cakin
     * @date 2021/3/14
     * @param args 命令行
     * @description: 以 产品类型 和 国家 多字段为关键字,进行计数
     */
    public static void main(String[] args) {
        List<Record> records = new ArrayList<>();
        records.add(new Record("电脑", "中国", "台式"));
        records.add(new Record("洗衣机", "韩国", "滚筒"));
        records.add(new Record("电脑", "中国", "笔记本"));
        records.add(new Record("洗衣机", "韩国", "一般"));
        records.add(new Record("电脑", "中国", "平板"));
        records.add(new Record("洗衣机", "中国", "笔记本"));
        records.add(new Record("电脑", "韩国", "滚筒"));
        records.add(new Record("电脑", "韩国", "平板"));
        records.add(new Record("洗衣机", "中国", "一般"));

        // 以 productType 和 country 两个字段为关键字,进行计数(Collectors.counting())
        Map<String, Long> countMap = records
                .stream()
                .collect(Collectors.groupingBy(o -> o.getProductType() + "_" + o.getCountry(), Collectors.counting()));

        List<Record> countRecords = countMap.keySet().stream().map(key -> {
            String[] temp = key.split("_");
            String productType = temp[0];
            String country = temp[1];
            Record record = new Record();
            record.setProductType(productType);
            record.setCountry(country);
            record.setCount(countMap.get(key).intValue());
            return record;
        }).collect(Collectors.toList());
        for (Record countRecord : countRecords) {
            System.out.println(countRecord);
        }
    }
}

class Record {
    /**
     * 产品类型
     */
    private String productType;
    /**
     * 国家
     */
    private String Country;
    /**
     * 风格
     */
    private String style;
    /**
     * 计数
     */
    private int count;

    public String getProductType() {
        return productType;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public String getCountry() {
        return Country;
    }

    public void setCountry(String country) {
        Country = country;
    }

    public Record() {
    }

    public Record(String productType, String country, String style) {
        this.style = style;
        this.productType = productType;
        this.Country = country;
    }


    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "Record{" +
                "productType='" + productType + '\'' +
                ", Country='" + Country + '\'' +
                ", count=" + count +
                '}';
    }
}

三 测试

Record{productType='洗衣机', Country='中国', count=2}
Record{productType='电脑', Country='中国', count=3}
Record{productType='洗衣机', Country='韩国', count=2}
Record{productType='电脑', Country='韩国', count=2}

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值