Code analysis of recording the continuous change time of data through Java
•
Java
This article mainly introduces the code analysis of recording the continuous change time of data through Java. It is introduced in great detail through the example code, which has a certain reference value for everyone's study or work. Friends in need can refer to it
1. Requirement: get the continuous change of count being null and non null
[{count=0,time=0},{count=10,time=1000},{count=20,time=2000},{count=30,time=3000},{count=40,time=4000},{count=null,time=5000},time=6000},time=7000},time=8000},time=9000},{count=100,time=10000},{count=110,time=11000},{count=120,time=12000},{count=130,time=13000},{count=140,time=14000}]
2. The code is as follows:
package com.stop; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * static boolean temp_flag; // 记录容器 public void execute(boolean flag) { if (temp_flag == flag) { // 没有变化 } else { if (flag == false) { // 上次是true,本次是false } else { // 上次是false本次是true } } } */ public class Test { public static List<Map<String,Object>> buildList() { List<Map<String,Object>> items = new ArrayList<>(); for(int i=0;i<5;i++) { Map<String,Object> map = new HashMap<>(); map.put("time",i*1000); map.put("count",i*10); items.add(map); } for(int i=5;i<10;i++) { Map<String,null); items.add(map); } for(int i=10;i<15;i++) { Map<String,i*10); items.add(map); } return items; } public static void main(String[] args) { // 构造数据 List<Map<String,Object>> items = buildList(); List<Map<String,Object>> list = new ArrayList<>(); boolean isStop = false;// 记录容器 for (int i = 0; i < items.size(); i++) { boolean flag = items.get(i).get("count") == null; if (i == 0) { Map<String,Object> map = new HashMap<>(); if (flag) { map.put("stop",items.get(i).get("time")); isStop = true; } else { map.put("recover",items.get(i).get("time")); } list.add(map); continue; } if (isStop == flag) { // 没有变化 } else { isStop = flag; Map<String,Object> map = new HashMap<>(); if (!flag) { // 上次是true,本次是false map.put("recover",items.get(i).get("time")); } else { // 上次是false本次是true map.put("stop",items.get(i).get("time")); } list.add(map); } } System.out.println(list); } }
3. Run main method results
[{recover=0},{stop=5000},{recover=10000}]
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码