网站首页 > 精选教程 正文
排序在系统中经常能用到,一般可以在数据库做排序,也可以在服务端做排序。在数据库一般使用 order by 排序。而服务端也是使用快排。本期使用汇总排序。
问题
统计销售数据,每个销售员都有对应的部门和销售量,现在要统计销售数据。
要求部门总销量递减排序,相同部门的也按照递减排序。
比如:
销售员 | 部门 | 销售额 |
A | 南部 | 100w |
B | 南部 | 20W |
C | 北部 | 30W |
D | 北部 | 70W |
E | 北部 | 40W |
F | 东部 | 150W |
根据汇总排序:
部门 | 销售额 |
东部 | 150W |
北部 | 130W |
南部 | 120W |
然后根据先按照部门总和排序,相同部门按照递减排序:
销售员 | 部门 | 销售额 |
F | 东部 | 150W |
D | 北部 | 70W |
E | 北部 | 40W |
C | 北部 | 30W |
E | 南部 | 100W |
F | 南部 | 20W |
解决方案
前期创建 model
public class SalesmanStatistic {
public SalesmanStatistic(String salesman, String department, Integer amount) {
this.salesman = salesman;
this.department = department;
this.amount = amount;
}
@Override
public String toString() {
return "{salesman='" + salesman + '\'' +
", department='" + department + '\'' +
", amount=" + amount +
'}';
}
private String salesman;
private String department;
private Integer amount;
}
添加数据
// 填充数据
List<SalesmanStatistic> list = new ArrayList<>();
SalesmanStatistic statistic1 = new SalesmanStatistic("A","南方",100);
SalesmanStatistic statistic2 = new SalesmanStatistic("B","南方",20);
SalesmanStatistic statistic3 = new SalesmanStatistic("C","北方",30);
SalesmanStatistic statistic4 = new SalesmanStatistic("D","北方",70);
SalesmanStatistic statistic5 = new SalesmanStatistic("E","北方",40);
SalesmanStatistic statistic6 = new SalesmanStatistic("F","东方",150);
list.add(statistic1);
list.add(statistic2);
list.add(statistic3);
list.add(statistic4);
list.add(statistic5);
list.add(statistic6);
使用两个 map,key 都是存部门,第一个 map 的 value 销售额的总额,第二个是存同部门的列表。
Map<String,Integer> sumMap = new HashMap<>();
Map<String, List<SalesmanStatistic>> listMap = new HashMap<>();
// 把数据放在 map 里面
for (SalesmanStatistic statistic : list) {
String key = statistic.getDepartment();
Integer amount = statistic.getAmount();
sumMap.put(key,sumMap.getOrDefault(key,0) + amount);
List<SalesmanStatistic> subList = listMap.get(key);
if (subList == null) {
subList = new ArrayList<>();
}
subList.add(statistic);
listMap.put(key,subList);
}
首先进行总量 sumMap 排序,就是对 sumMap 的 value 进行排序:
// 对总和 sumMap 排序
List<Map.Entry<String,Integer>> sumMapList = new ArrayList<>(sumMap.entrySet());
sumMapList.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
以上获取到总和 sumMap 的列表,从大到小排列,然后遍历每个数据,根据 key 匹配到 listMap 的 key。首先获取对 listMap 的 value 列表进行排序,然后把 list 添加到总的集合里面。
List<SalesmanStatistic> list = new ArrayList<>();
for (Map.Entry<String,Integer> entry : sumMapList) {
List<SalesmanStatistic> list1 = listMap.get(entry.getKey());
list1.sort((o1, o2) -> o2.getAmount().compareTo(o1.getAmount()));
list.addAll(list1);
}
list.stream().forEach(list3 -> System.out.println(list3.toString()));
打印输出结果:
{salesman='F', department='东方', amount=150}
{salesman='D', department='北方', amount=70}
{salesman='E', department='北方', amount=40}
{salesman='C', department='北方', amount=30}
{salesman='A', department='南方', amount=100}
{salesman='B', department='南方', amount=20}
总结
根据部门汇总和进行排序,然后每个部门也按照从大到小排序。这里使用到 map 的键值对属性。流程如下:
- 使用 sumMap 存储部门总数以及使用 listMap 存储部门信息。
- 对 sumMap 的 value 排序,把 map.entrySet 放在一个集合做排序。
- 根据排序后的 sumMap 的 key 找到 listMap 的 value。先对列表排序,最后放在集合中。
- 上一篇: 经典算法之冒泡排序
- 下一篇: 计算机入门必备算法——快速排序法
猜你喜欢
- 2024-12-02 javaCPU飙升怎么解决
- 2024-12-02 【技术管理】如何给任务排优先级?
- 2024-12-02 Java 应用程序的 CPU 使用率飙升原因分析
- 2024-12-02 八种经典排序算法总结(含动画演示)
- 2024-12-02 计算机入门必备算法——快速排序法
- 2024-12-02 经典算法之冒泡排序
- 2024-12-02 拜托,别再问我什么是堆了! 这篇文章让你彻底明白!
- 2024-12-02 推荐一款免费排课软件
- 2024-12-02 腾讯工作7年被裁,3个月逆袭字节跳动,对此我想对大家说
- 2024-12-02 一遍记住 8 种排序算法与 Java 代码实现
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- nginx反向代理 (57)
- nginx日志 (56)
- nginx限制ip访问 (62)
- mac安装nginx (55)
- java和mysql (59)
- java中final (62)
- win10安装java (72)
- java启动参数 (64)
- java链表反转 (64)
- 字符串反转java (72)
- java逻辑运算符 (59)
- java 请求url (65)
- java信号量 (57)
- java定义枚举 (59)
- java字符串压缩 (56)
- java中的反射 (59)
- java 三维数组 (55)
- java插入排序 (68)
- java线程的状态 (62)
- java异步调用 (55)
- java中的异常处理 (62)
- java锁机制 (54)
- java静态内部类 (55)
- java怎么添加图片 (60)
- java 权限框架 (55)
本文暂时没有评论,来添加一个吧(●'◡'●)