需求
一个对象list,每个对象中包含开始时间、结束时间、等级三个属性,现在要求判断list中每条数据之间是否存在交集的现象,如果存在,就按照两条数据之间的差集、交集的关系,生成一个新的list。需求如图所示:
如图,list中原本只有红色的1、2、3三条数据,现在要求分隔为蓝色的1、2、3、4、5五条数据,并且交集部分,每条数据等级取最大值。
思路
第一条数据:d1、第二条数据:d2
1、对list按照开始时间和结束时间两个属性正序排序。
2、对整个list的数据两两对比,判断d2的开始时间是否大于d1的开始时间并且小于d1的结束时间,如果是,则存在交集,对两条数据进行分隔,形成新的数据结果。
3、分隔方法:d1的开始时间 ~ d2的开始时间 | d2的开始时间 ~ d1的结束时间>d2的结束时间?d2结束时间:d1结束时间 | d2结束时间 ~ d1结束时间。
代码实现
1、准备list数据,开始时间和结束时间都是用的时间戳格式,方便对比
"stime","etime","level"
"1641553083000","1641567483000","1"
"1641755759000","1641770799000","1"
"1641844800000","1641859812000","1"
"1641930857000","1641948197000","1"
"1642016936000","1642034126000","1"
"1642101897000","1642122000000","1"
"1642132800000","1642159162000","2"
"1642194000000","1642251235000","2"
"1642276800000","1642292972000","2"
"1642305798000","1642338172000","2"
"1642366800000","1642383279000","2"
"1642385743000","1642410887000","2"
"1642449600000","1642514400000","2"
"1642532418000","1642597200000","1"
"1642532958000","1642588274000","2"
"1642560737000","1642588274000","3"
"1642622400000","1642647600000","1"
"1642622781000","1642647600000","2"
"1642651200000","1642663086000","2"
"1642663888000","1642679318000","3"
"1642706412000","1642756183000","2"
"1642798800000","1642814491000","3"
"1642817635000","1642847093000","3"
"1642881600000","1642924800000","3"
"1642932000000","1642933499000","3"
"1642971600000","1643030605000","3"
"1643053962000","1643103522000","3"
"1643108209000","1643122800000","3"
"1643140800000","1643157743000","3"
"1643227338000","1643270400000","3"
"1643320800000","1643353200000","3"
2、java实现
package com.test;
import com.alibaba.fastjson.JSON;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) {
List<Map<String,Object>> list = new ArrayList<>();
/**
* 给list添加数据
*/
List<Map<String,Object>> newList = check(list);
newList.stream().forEach(e -> {
System.out.println(JSON.toJSONString(e));
});
}
public static List<Map<String, Object>> check(List<Map<String, Object>> intervalList) {
List<Map<String, Object>> interval = new ArrayList<>();
interval.add(intervalList.get(0));
for (int i = 0; i < intervalList.size(); i++) {
Boolean flag = true;
Map<String, Object> m2 = intervalList.get(i);
for (int j =0;j<interval.size();j++){
Map<String, Object> m1 = interval.get(j);
Long m1stime = new BigDecimal(String.valueOf(m1.get("stime"))).longValue();
Long m1etime = new BigDecimal(String.valueOf(m1.get("etime"))).longValue();
Long m1level = new BigDecimal(String.valueOf(m1.get("level"))).longValue();
Long m2stime = new BigDecimal(String.valueOf(m2.get("stime"))).longValue();
Long m2etime = new BigDecimal(String.valueOf(m2.get("etime"))).longValue();
// 存在交集
if (m2stime >= m1stime && m2stime < m1etime) {
flag = false;
interval.remove(j);
//第一个差集
if (m2stime>m1stime){
Map<String,Object> m_ = new HashMap<>();
m_.put("stime",m1stime);
m_.put("etime",m2stime);
m_.put("level",m1level);
interval.add(m_);
}
//第一个交集
if (m2etime>=m1etime){
Map<String,Object> m_ = m2;
interval.add(m_);
} else {
Map<String,Object> m_1 = m2;
Map<String,Object> m_2 = new HashMap<>();
m_2.put("stime",m2etime);
m_2.put("etime",m1etime);
m_2.put("level",m1level);
interval.add(m_1);
interval.add(m_2);
}
}
}
if (flag) {
interval.add(m2);
}
}
return interval;
}
}
关注我的公众号,学习更多知识。