自定义泛型方法, 三级排序, low版,待升级
package com.jd.dashboard.util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jd.big.data.common.dashboard.model.DeptMonitorStoreVo;
import com.jd.big.data.common.dashboard.model.DeptMonitorVo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 分部门级别的排序的排序
* Created by niewj on 2016/10/20.
*/
public class SortUtil {
/**
* 按照部门父子顺序排序:1.父子部门;2.同级别内按照value值倒序
*
* @param list 所有的节点
* @return
*/
public static List<? extends DeptMonitorVo> sortAllDeptLevelList(List<? extends DeptMonitorVo> list) {
if (list == null || list.isEmpty()) {
return list;
}
/**
* 1.分成4组
*/
List<DeptMonitorVo> listBu = new ArrayList<>();
List<DeptMonitorVo> listDept1 = new ArrayList<>();
List<DeptMonitorVo> listDept2 = new ArrayList<>();
List<DeptMonitorVo> listDept3 = new ArrayList<>();
for (DeptMonitorVo vo : list) {
if (vo.getBu_id() != null && vo.getDept_id_1() == null) {
listBu.add(vo);
} else if (vo.getBu_id() != null && vo.getDept_id_1() != null && vo.getDept_id_2() == null) {
listDept1.add(vo);
} else if (vo.getBu_id() != null && vo.getDept_id_1() != null && vo.getDept_id_2() != null && vo.getDept_id_3() == null) {
listDept2.add(vo);
} else if (vo.getBu_id() != null && vo.getDept_id_1() != null && vo.getDept_id_2() != null && vo.getDept_id_3() != null) {
listDept3.add(vo);
}
}
/**
* 2.每组排序
*/
if (!listBu.isEmpty()) {
Collections.sort(listBu);
}
if (!listDept1.isEmpty()) {
Collections.sort(listDept1);
}
if (!listDept2.isEmpty()) {
Collections.sort(listDept2);
}
if (!listDept3.isEmpty()) {
Collections.sort(listDept3);
}
/**
* 3.认亲-认上层节点-追加顺序
*/
List<DeptMonitorVo> result2 = sortParentAndSon3(listDept3, listDept2);
List<DeptMonitorVo> result1 = sortParentAndSon2(result2, listDept1);
List<DeptMonitorVo> result0 = sortParentAndSon1(result1, listBu);
return result0;
}
/**
* 两层排序,然后返回父亲列表
*
* @param sonList
* @param parentList
*/
private static List<DeptMonitorVo> sortParentAndSon3(List<DeptMonitorVo> sonList, List<DeptMonitorVo> parentList) {
if (sonList.isEmpty()) {
return parentList;
}
List<DeptMonitorVo> result = new ArrayList<>();
for (DeptMonitorVo parentVo : parentList) {
// 1. 先加入父亲
result.add(parentVo);
long dept2 = parentVo.getDept_id_2().longValue();
// 2. 再便利子节点中的符合项, 加入
for (DeptMonitorVo sonVo : sonList) {
long sonDept2 = sonVo.getDept_id_2().longValue();
if (dept2 == sonDept2) {
result.add(sonVo);
}
}
}
return result;
}
/**
* 两层排序,然后返回父亲列表
*
* @param sonList
* @param parentList
*/
private static List<DeptMonitorVo> sortParentAndSon2(List<DeptMonitorVo> sonList, List<DeptMonitorVo> parentList) {
if (sonList.isEmpty()) {
return parentList;
}
List<DeptMonitorVo> result = new ArrayList<>();
for (DeptMonitorVo parentVo : parentList) {
result.add(parentVo); // 1. 先加入父亲
long dept1 = parentVo.getDept_id_1().longValue();
for (DeptMonitorVo sonVo : sonList) { // 2. 再便利子节点中的符合项, 加入
long sonDept1 = sonVo.getDept_id_1().longValue();
if (dept1 == sonDept1) {
result.add(sonVo);
}
}
}
return result;
}
/**
* 两层排序,然后返回父亲列表
*
* @param sonList
* @param parentList
*/
private static List<DeptMonitorVo> sortParentAndSon1(List<DeptMonitorVo> sonList, List<DeptMonitorVo> parentList) {
if (sonList.isEmpty()) {
return parentList;
}
List<DeptMonitorVo> result = new ArrayList<>();
for (DeptMonitorVo parentVo : parentList) {
// 1. 先加入父亲
result.add(parentVo);
long dept0 = parentVo.getBu_id().longValue();
// 2. 再便利子节点中的符合项, 加入
for (DeptMonitorVo sonVo : sonList) {
long sonDept0 = sonVo.getBu_id().longValue();
if (dept0 == sonDept0) {
result.add(sonVo);
}
}
}
return result;
}
public static void main(String[] args) {
String voListString = "[{\"dept_name\":\"3D周边CX部\",\"country_all_day\":1.0,\"bj\":1.0,\"sh\":1.0,\"gz\":1.0,\"cd\":1.0,\"wh\":1.0,\"sy\":1.0,\"xa\":1.0,\"ga\":1.0,\"hz\":1.0,\"bu_id\":2420,\"dept_id_1\":1324,\"dept_id_2\":1334},{\"dept_name\":\"ZNSMCX部\",\"country_all_day\":0.96163,\"bj\":0.9632,\"sh\":0.9677,\"gz\":0.94996,\"cd\":0.96186,\"wh\":0.96438,\"sy\":0.963,\"xa\":0.96552,\"ga\":0.96366,\"hz\":0.97454,\"bu_id\":2420,\"dept_id_1\":1324,\"dept_id_2\":1333},{\"dept_name\":\"SMYYCX部\",\"country_all_day\":0.93846,\"bj\":0.94308,\"sh\":0.94065,\"gz\":0.93518,\"cd\":0.94158,\"wh\":0.92635,\"sy\":0.9362,\"xa\":0.94435,\"ga\":0.93955,\"hz\":0.95217,\"bu_id\":2420,\"dept_id_1\":1324,\"dept_id_2\":1332},{\"dept_name\":\"SM业务部\",\"country_all_day\":0.93419,\"bj\":0.94633,\"sh\":0.94231,\"gz\":0.93303,\"cd\":0.92651,\"wh\":0.91655,\"sy\":0.91778,\"xa\":0.92837,\"ga\":0.93629,\"hz\":0.95856,\"bu_id\":2420,\"dept_id_1\":1324},{\"dept_name\":\"SMPOP及配件CX部\",\"country_all_day\":0.93051,\"bj\":0.9493,\"sh\":0.93414,\"gz\":0.92707,\"cd\":0.92507,\"wh\":0.91526,\"sy\":0.91529,\"xa\":0.93691,\"ga\":0.92456,\"hz\":0.95797,\"bu_id\":2420,\"dept_id_1\":1324,\"dept_id_2\":1335},{\"dept_name\":\"平板CX部\",\"country_all_day\":0.90506,\"bj\":0.91987,\"sh\":0.92736,\"gz\":0.92255,\"cd\":0.87691,\"wh\":0.86422,\"sy\":0.85504,\"xa\":0.85542,\"ga\":0.91938,\"hz\":0.94745,\"bu_id\":2420,\"dept_id_1\":1324,\"dept_id_2\":1776},{\"dept_name\":\"电子书\",\"country_all_day\":1.0,\"bj\":1.0,\"sh\":1.0,\"gz\":1.0,\"cd\":1.0,\"wh\":1.0,\"sy\":1.0,\"xa\":1.0,\"bu_id\":2420,\"dept_id_1\":31,\"dept_id_2\":43},{\"dept_name\":\"自营图书CX部\",\"country_all_day\":0.96973,\"bj\":0.9522,\"sh\":0.97175,\"gz\":0.97694,\"cd\":0.977,\"wh\":0.97683,\"sy\":0.97297,\"xa\":0.97296,\"bu_id\":2420,\"dept_id_1\":31,\"dept_id_2\":41},{\"dept_name\":\"图书音像YW部\",\"country_all_day\":0.968,\"bj\":0.95127,\"sh\":0.97,\"gz\":0.97537,\"cd\":0.97542,\"wh\":0.97583,\"sy\":0.96917,\"xa\":0.96876,\"bu_id\":2420,\"dept_id_1\":31},{\"dept_name\":\"音像CX部\",\"country_all_day\":0.89063,\"bj\":0.90068,\"sh\":0.8967,\"gz\":0.91775,\"cd\":0.91461,\"wh\":0.92825,\"sy\":0.77064,\"xa\":0.70671,\"bu_id\":2420,\"dept_id_1\":31,\"dept_id_2\":42},{\"dept_name\":\"办公CX部\",\"country_all_day\":0.95925,\"bj\":0.97993,\"sh\":0.95771,\"gz\":0.95925,\"cd\":0.95966,\"wh\":0.95505,\"sy\":0.95023,\"xa\":0.95304,\"ga\":0.94573,\"hz\":0.96364,\"bu_id\":2420,\"dept_id_1\":30,\"dept_id_2\":39},{\"dept_name\":\"外设CX部\",\"country_all_day\":0.94805,\"bj\":0.95503,\"sh\":0.94445,\"gz\":0.94828,\"cd\":0.94679,\"wh\":0.93955,\"sy\":0.94585,\"xa\":0.94658,\"ga\":0.95417,\"hz\":0.95216,\"bu_id\":2420,\"dept_id_1\":30,\"dept_id_2\":40},{\"dept_name\":\"电脑配件CX部\",\"country_all_day\":0.93659,\"bj\":0.9486,\"sh\":0.93087,\"gz\":0.92917,\"cd\":0.94604,\"wh\":0.94384,\"sy\":0.93015,\"xa\":0.93837,\"ga\":0.93589,\"hz\":0.94406,\"bu_id\":2420,\"dept_id_1\":30,\"dept_id_2\":336},{\"dept_name\":\"电脑办公业务部\",\"country_all_day\":0.91942,\"bj\":0.94834,\"sh\":0.91891,\"gz\":0.90477,\"cd\":0.91616,\"wh\":0.89693,\"sy\":0.92615,\"xa\":0.92688,\"ga\":0.93079,\"hz\":0.92965,\"bu_id\":2420,\"dept_id_1\":30},{\"dept_name\":\"整机CX部\",\"country_all_day\":0.85047,\"bj\":0.91414,\"sh\":0.85749,\"gz\":0.81637,\"cd\":0.83107,\"wh\":0.79688,\"sy\":0.87217,\"xa\":0.87578,\"ga\":0.88881,\"hz\":0.87836,\"bu_id\":2420,\"dept_id_1\":30,\"dept_id_2\":37},{\"dept_name\":\"直供业务部\",\"country_all_day\":0.84758,\"bj\":0.83333,\"sh\":0.84127,\"gz\":0.80693,\"cd\":0.94118,\"wh\":0.87619,\"sy\":0.89474,\"xa\":0.72727,\"ga\":0.89209,\"hz\":0.66667,\"bu_id\":2420,\"dept_id_1\":1,\"dept_id_2\":1553},{\"dept_name\":\"通讯业务二部\",\"country_all_day\":0.76592,\"bj\":0.77973,\"sh\":0.74857,\"gz\":0.75667,\"cd\":0.77958,\"wh\":0.76166,\"sy\":0.7784,\"xa\":0.77247,\"ga\":0.77877,\"hz\":0.76151,\"bu_id\":2420,\"dept_id_1\":1,\"dept_id_2\":738},{\"dept_name\":\"TX业务部\",\"country_all_day\":0.65548,\"bj\":0.64468,\"sh\":0.62067,\"gz\":0.67024,\"cd\":0.67957,\"wh\":0.6631,\"sy\":0.65866,\"xa\":0.66686,\"ga\":0.65447,\"hz\":0.62753,\"bu_id\":2420,\"dept_id_1\":1},{\"dept_name\":\"运营商业务部\",\"country_all_day\":0.55414,\"bj\":0.52975,\"sh\":0.55859,\"gz\":0.53727,\"cd\":0.69676,\"wh\":0.50726,\"sy\":0.51485,\"xa\":0.56237,\"ga\":0.51848,\"hz\":0.55759,\"bu_id\":2420,\"dept_id_1\":1,\"dept_id_2\":1524},{\"dept_name\":\"通讯业务一部\",\"country_all_day\":0.54539,\"bj\":0.52234,\"sh\":0.50463,\"gz\":0.57716,\"cd\":0.57954,\"wh\":0.56136,\"sy\":0.54289,\"xa\":0.55968,\"ga\":0.52331,\"hz\":0.51473,\"bu_id\":2420,\"dept_id_1\":1,\"dept_id_2\":727},{\"dept_name\":\"2D事业部\",\"country_all_day\":0.82657,\"bj\":0.84881,\"sh\":0.82391,\"gz\":0.81995,\"cd\":0.83711,\"wh\":0.81759,\"sy\":0.83341,\"xa\":0.83206,\"ga\":0.81426,\"hz\":0.82026,\"bu_id\":2420}]";
List<DeptMonitorStoreVo> result = new Gson().fromJson(voListString, new TypeToken<List<DeptMonitorStoreVo>>() {
}.getType());
List<? extends DeptMonitorVo> result0 = SortUtil.sortAllDeptLevelList(result);
System.out.println(new Gson().toJson(result0));
}
}
自定义泛型方法, 三级排序, low版,待升级的更多相关文章
- 重写Oracle的wm_concat函数,自定义分隔符、排序
oracle中,wm_concat函数是一个聚合函数,和mysql中的group_concat函数类似,不过group_concat函数比较强大,可以定义分隔符和排序,当然所谓强大是相对的,这里假使我 ...
- win10预览版9926升级10049操作步骤
文章转自:豆豆系统收藏备用 win10预览版系统安装的用户非常多,现在最新版本已经到了10049,但是之前很多装了9926版本或者10041版本的同学在通过系统自动更新的时候,且发现,微软官方提供的速 ...
- MapReduce自定义二次排序流程
每一条记录开始是进入到map函数进行处理,处理完了之后立马就入自定义分区函数中对其进行分区,当所有输入数据经过map函数和分区函数处理完之后,就调用自定义二次排序函数对其进行排序. MapReduce ...
- Java基础知识强化之集合框架笔记69:Collections类之ArrayList存储自自定义对象并排序的案例
1. ArrayList存储自自定义对象并排序的案例: ArrayList存储自自定义对象,并使用Collections对ArrayList存储基本包装类的元素排序. 2. 代码实现: (1)Stud ...
- Hadoop学习之自定义二次排序
一.概述 MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往 往有要对reduce输出结果进行二次排 ...
- 功能测试很low?不能升级到高级测试工程师?
功能测试很low?不能升级到高级测试工程师? 功能测试很low?功能测试很简单?功能测试就是黑盒测试?功能测试没有技术含量?功能测试工资低?只会功能测试没有竞争力?功能测试这活初中生都可以干?功能测试 ...
- mac版chrome升级到Version 65.0.3325.18后无法打开百度bing搜狗
mac版本chrome升级到Version 65.0.3325.18后发现突然无法访问百度,搜狗,bing,神马等一系列的国内搜索引擎网站.连百度的儿子们比如知道,百度百科都无法访问. 1.首先想到的 ...
- 【转载】C#中自定义Sort的排序规则IComparable接口
C#中的List集合在排序的时候,如果不使用Lambda表达式进行排序的话,一般调用Sort()方法进行排序,如果希望Sort()方法排序后的结果跟我们预想的效果一致或者按照我们自定义的规则排序,则需 ...
- java泛型-自定义泛型方法与类型推断总结
下面是自定义泛型方法的练习: package com.mari.generic; import java.util.ArrayList; import java.util.Collection; im ...
随机推荐
- spring-boot-route(十三)整合RabbitMQ
这篇是SpringBoot整合消息队列的第一篇文章,我们详细介绍下消息队列的相关内容. 消息队列简介 1. 什么是消息队列 MQ(Message Quene):通过典型的生产者和消费者模型,生产者不断 ...
- linux内核输入子系统分析
1.为何引入input system? 以前我们写一些输入设备(键盘.鼠标等)的驱动都是采用字符设备.混杂设备处理的.问题由此而来,Linux开源社区的大神们看到了这大量输入设备如此分散不堪,有木有可 ...
- postgresql 和 mysql 数据库备份恢复以及时区问题
概要 postgesql 12 备份/恢复脚本 时区设置 mysql 5.6 备份/恢复脚本 时区设置 概要 postgresql 和 mysql 是最常用的 2 种开源关系数据库, 很多项目也会优先 ...
- 多测师讲解selenium ——切换窗口——打印句柄_高级讲师肖sir
(一)同一个窗口打开两个浏览器 from selenium import webdriverfrom time import sleepdrvier=webdriver.Chrome()url='ht ...
- 在VC6.0下运行C语言程序,以及编程入门必备的常识类小知识!
今天给大家分享在VC6.0环境下编写C语言程序的基本步骤,为初学者打开学习C语言的第一道门.具体步骤如下(如果需要软件资源,可以留言): 1)新建工作区 依次点击 文件--新建--工作区 或是Ctrl ...
- 【博弈论】51Nod 1534 棋子游戏
题目内容 波雷卡普和瓦西里喜欢简单的逻辑游戏.今天他们玩了一个游戏,这个游戏在一个很大的棋盘上进行,他们每个人有一个棋子.他们轮流移动自己的棋子,波雷卡普先开始.每一步移动中,波雷卡普可以将他的棋子从 ...
- es 讲义
链接: https://pan.baidu.com/s/193jObXs85OZcvAHDycUXlw 提取码: ewbp
- lumen路由
$router->get('/', function () use ($router) { return config('options.author'); }); $router->ge ...
- 记一次flink入门学习笔记
团队有几个系统数据量偏大,且每天以几万条的数量累增.有一个系统每天需要定时读取数据库,并进行相关的业务逻辑计算,从而获取最新的用户信息,定时任务的整个耗时需要4小时左右.由于定时任务是夜晚执行,目前看 ...
- pwn之栈缓冲区溢出漏洞(入门)
题目ret2text 题目信息确认 使用file命令查看文件类型 root@CTF:/home/# file ret2text ret2text: ELF 32-bit LSB executable, ...