通过java记录数据持续变化时间代码解析

发布时间:2020-10-20 10:53:24 作者:郑某人1
来源:脚本之家 阅读:159

这篇文章主要介绍了通过java记录数据持续变化时间代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.需求:获取count为null和不为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.代码如下:

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,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) {
    // 构造数据
    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.运行main方法结果

[{recover=0}, {stop=5000}, {recover=10000}]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. 如何通过持续ping来检测网络质量
  2. java如何通过XPath解析xml节点

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java 记录 数据

上一篇:Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】

下一篇:Android Studio连接手机设备教程

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》