实体类
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}