栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

这篇文章主要介绍了通过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;


public class Test {
  public static List> buildList() {
    List> items = new ArrayList<>();
    for(int i=0;i<5;i++) {
      Map map = new HashMap<>();
      map.put("time", i*1000);
      map.put("count", i*10);
      items.add(map);
    }
    for(int i=5;i<10;i++) {
      Map map = new HashMap<>();
      map.put("time", i*1000);
      map.put("count", null);
      items.add(map);
    }
    for(int i=10;i<15;i++) {
      Map 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> items = buildList();
    
    List> 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 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 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}]

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/135456.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号