java8 stream处理集合

实体类

package com.ahut.common.utils.bean;

import java.math.BigDecimal;

/**
 * desc : 接口监控实体
 * create_user : cheng
 * create_date : 2019/3/15 10:37
 */
public class InterfaceMonitorEntity {

    /**
     * 接口名称
     */
    private String interfaceName;
    /**
     * 状态码
     */
    private int status;
    /**
     * 调用次数
     */
    private int callCount;
    /**
     * 分数
     */
    private BigDecimal score;

    public InterfaceMonitorEntity(String interfaceName, int status, int callCount, BigDecimal score) {
        this.interfaceName = interfaceName;
        this.status = status;
        this.callCount = callCount;
        this.score = score;
    }

    @Override
    public String toString() {
        return "InterfaceMonitorEntity{" +
                "interfaceName='" + interfaceName + '\'' +
                ", status=" + status +
                ", callCount=" + callCount +
                ", score=" + score +
                '}';
    }

    public String getInterfaceName() {
        return interfaceName;
    }

    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getCallCount() {
        return callCount;
    }

    public void setCallCount(int callCount) {
        this.callCount = callCount;
    }

    public BigDecimal getScore() {
        return score;
    }

    public void setScore(BigDecimal score) {
        this.score = score;
    }
}

单字段分组

    @Test
    public void testGroupBy() {
        List<InterfaceMonitorEntity> monitorList = new ArrayList<>(5);

        monitorList.add(new InterfaceMonitorEntity("create", 0, 1000, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("create", -1, 1, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("delete", 0, 10, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("update", 0, 100, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", 0, 10000, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", 0, 10001, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", -1, 10002, new BigDecimal(1)));

        // 单字段分组(依据interfaceName进行分组)
        Map<String, List<InterfaceMonitorEntity>> monitorMap1 = monitorList.stream().collect(Collectors.groupingBy(InterfaceMonitorEntity::getInterfaceName));
        for (Map.Entry<String, List<InterfaceMonitorEntity>> entry : monitorMap1.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
	}

输出

search : [InterfaceMonitorEntity{interfaceName='search', status=0, callCount=10000}, InterfaceMonitorEntity{interfaceName='search', status=0, callCount=10001}, InterfaceMonitorEntity{interfaceName='search', status=-1, callCount=10002}]
update : [InterfaceMonitorEntity{interfaceName='update', status=0, callCount=100}]
create : [InterfaceMonitorEntity{interfaceName='create', status=0, callCount=1000}, InterfaceMonitorEntity{interfaceName='create', status=-1, callCount=1}]
delete : [InterfaceMonitorEntity{interfaceName='delete', status=0, callCount=10}]

多字段分组

    @Test
    public void testGroupBy() {
        List<InterfaceMonitorEntity> monitorList = new ArrayList<>(5);

        monitorList.add(new InterfaceMonitorEntity("create", 0, 1000, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("create", -1, 1, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("delete", 0, 10, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("update", 0, 100, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", 0, 10000, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", 0, 10001, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", -1, 10002, new BigDecimal(1)));

        // 多字段分组
        Map<String, List<InterfaceMonitorEntity>> monitorMap2 = monitorList.stream().collect(Collectors.groupingBy(this::multiField));
        for (Map.Entry<String, List<InterfaceMonitorEntity>> entry : monitorMap2.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
	}
	
    /**
     * desc : 组合字段
     * create_user : cheng
     * create_date : 2019/3/18 16:13
     */
    private String multiField(InterfaceMonitorEntity entity) {
        return entity.getInterfaceName() + entity.getStatus();
    }

输出

update0 : [InterfaceMonitorEntity{interfaceName='update', status=0, callCount=100}]
create-1 : [InterfaceMonitorEntity{interfaceName='create', status=-1, callCount=1}]
search0 : [InterfaceMonitorEntity{interfaceName='search', status=0, callCount=10000}, InterfaceMonitorEntity{interfaceName='search', status=0, callCount=10001}]
create0 : [InterfaceMonitorEntity{interfaceName='create', status=0, callCount=1000}]
search-1 : [InterfaceMonitorEntity{interfaceName='search', status=-1, callCount=10002}]
delete0 : [InterfaceMonitorEntity{interfaceName='delete', status=0, callCount=10}]

分组统计数量

    @Test
    public void testGroupBy() {
        List<InterfaceMonitorEntity> monitorList = new ArrayList<>(5);

        monitorList.add(new InterfaceMonitorEntity("create", 0, 1000, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("create", -1, 1, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("delete", 0, 10, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("update", 0, 100, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", 0, 10000, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", 0, 10001, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", -1, 10002, new BigDecimal(1)));

        // 分组统计
        Map<String, Long> monitorMap3 = monitorList.stream().collect(Collectors.groupingBy(InterfaceMonitorEntity::getInterfaceName, Collectors.counting()));
        for (Map.Entry<String, Long> entry : monitorMap3.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }

输出

search : 3
update : 1
create : 2
delete : 1

多字段排序

    @Test
    public void testGroupBy() {
        List<InterfaceMonitorEntity> monitorList = new ArrayList<>(5);

        monitorList.add(new InterfaceMonitorEntity("create", 0, 1000, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("create", -1, 1, new BigDecimal(2)));
        monitorList.add(new InterfaceMonitorEntity("delete", 0, 10, new BigDecimal(3)));
        monitorList.add(new InterfaceMonitorEntity("update", 0, 100, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", 0, 10000, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", 0, 10001, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", -1, 10002, new BigDecimal(1)));
                
        // 多字段排序
        monitorList.stream()
                .sorted(Comparator.comparing(InterfaceMonitorEntity::getStatus)
                        .thenComparing(InterfaceMonitorEntity::getCallCount)
                        .thenComparing(InterfaceMonitorEntity::getScore))
                .forEach(System.out::println);
    }

输出

InterfaceMonitorEntity{interfaceName='create', status=-1, callCount=1, score=2}
InterfaceMonitorEntity{interfaceName='search', status=-1, callCount=10002, score=1}
InterfaceMonitorEntity{interfaceName='delete', status=0, callCount=10, score=3}
InterfaceMonitorEntity{interfaceName='update', status=0, callCount=100, score=1}
InterfaceMonitorEntity{interfaceName='create', status=0, callCount=1000, score=1}
InterfaceMonitorEntity{interfaceName='search', status=0, callCount=10000, score=1}
InterfaceMonitorEntity{interfaceName='search', status=0, callCount=10001, score=1}

倒序排序

    @Test
    public void testGroupBy() {
        List<InterfaceMonitorEntity> monitorList = new ArrayList<>(5);

        monitorList.add(new InterfaceMonitorEntity("create", 0, 1000, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("create", -1, 1, new BigDecimal(2)));
        monitorList.add(new InterfaceMonitorEntity("delete", 0, 10, new BigDecimal(3)));
        monitorList.add(new InterfaceMonitorEntity("update", 0, 100, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", 0, 10000, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", 0, 10001, new BigDecimal(1)));
        monitorList.add(new InterfaceMonitorEntity("search", -1, 10002, new BigDecimal(1)));

        // 多字段排序
        monitorList.stream()
                .sorted(Comparator.comparing(InterfaceMonitorEntity::getCallCount).reversed())
                .forEach(System.out::println);
    }

输出

InterfaceMonitorEntity{interfaceName='search', status=-1, callCount=10002, score=1}
InterfaceMonitorEntity{interfaceName='search', status=0, callCount=10001, score=1}
InterfaceMonitorEntity{interfaceName='search', status=0, callCount=10000, score=1}
InterfaceMonitorEntity{interfaceName='create', status=0, callCount=1000, score=1}
InterfaceMonitorEntity{interfaceName='update', status=0, callCount=100, score=1}
InterfaceMonitorEntity{interfaceName='delete', status=0, callCount=10, score=3}
InterfaceMonitorEntity{interfaceName='create', status=-1, callCount=1, score=2}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值