Code analysis of recording data duration through Java

Time:2020-6-28

This article mainly introduces the code analysis of the continuous change time of data recorded by Java. In this article, the example code is introduced in detail, which has certain reference learning value for everyone’s study or work. You can refer to the following for your friends

1. Requirement: obtain the continuous change of count 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},
{count=null, time=6000},
{count=null, time=7000},
{count=null, time=8000},
{count=null, 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; // record container
  public void execute(boolean flag) {
    if (temp_flag == flag) {
      //No change
    } else {
      if (flag == false) {
        //True last time, false this time
      } else {
        //Last time is false this time is 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,Object> map = new HashMap<>();
      map.put("time", i*1000);
      map.put("count", null);
      items.add(map);
    }
    for(int i=10;i<15;i++) {
      Map<String,Object> map = new HashMap<>();
      map.put("time", i*1000);
      map.put("count", i*10);
      items.add(map);
    }
    return items;
  }



  public static void main(String[] args) {
    //Construction data
    List<Map<String, Object>> items = buildList();
    
    List<Map<String, Object>> list = new ArrayList<>();
    Boolean isstop = false; // record container
    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) {
        //No change
      } else {
        isStop = flag;
        Map<String, Object> map = new HashMap<>();
        if (!flag) {
          //True last time, false this time
          map.put("recover", items.get(i).get("time"));
        } else {
          //Last time is false this time is true
          map.put("stop", items.get(i).get("time"));
        }
        list.add(map);
      }
    }
    System.out.println(list);
  }

}

3. Result of running main method


[{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 can support developepaer more.