Java 集合统计:获取指定日期范围内每个 ID 的 rksl 和 cksl

本文将介绍如何使用 Java 集合来统计给定日期范围内每个 ID 的 rkslcksl,即使该日期没有对应的记录,也会将其统计为 0。

示例数据:

[{'id': '4028805f88c2982d0188c2ebb56303a9', 'entranceName': '一号出入口', 'rksl': 72, 'cksl': 0, 'year': 2023, 'month': 6, 'day': 26},
 {'id': '4028805f88c2982d0188c2ebb56303a9', 'entranceName': '一号出入口', 'rksl': 37, 'cksl': 0, 'year': 2023, 'month': 5, 'day': 5},
 {'id': '4028805f88c2982d0188c2ec3a4b03b5', 'entranceName': '二号出入口', 'rksl': 0, 'cksl': 0, 'year': null, 'month': null, 'day': null},
 {'id': '4028805f88c2982d0188c2ecff5f03c1', 'entranceName': '三号出入口', 'rksl': 0, 'cksl': 0, 'year': null, 'month': null, 'day': null}]

目标:

获取 kssj - jssj 范围内每一天的各 ID 的 rkslcksl,没有的日期各数量为 0。

实现步骤:

  1. 创建 Map: 使用一个 Map 来存储数据,以日期为键,以一个包含所有 ID 的 Map 为值。

  2. 遍历集合: 遍历 EntranceTrafficVO 集合中的每个对象,对于每个对象:

    • 将它的日期转换为字符串。
    • 根据日期在 Map 中查找对应的 Map,如果找不到,则新建一个包含所有 ID 的 Map,并将其放入 Map 中。
    • 将该对象的 rkslcksl 加到对应 ID 的值中。
  3. 遍历 Map: 遍历 Map,生成包含所有日期和每个 ID 的 rkslcksl 的二维数组。

代码实现:

import java.text.SimpleDateFormat;
import java.util.*;

public class EntranceTrafficDemo {

    public static void main(String[] args) throws Exception {
        List<EntranceTrafficVO> list = new ArrayList<>();
        list.add(new EntranceTrafficVO('4028805f88c2982d0188c2ebb56303a9', '一号出入口', 72, 0, 2023, 6, 26));
        list.add(new EntranceTrafficVO('4028805f88c2982d0188c2ebb56303a9', '一号出入口', 37, 0, 2023, 5, 5));
        list.add(new EntranceTrafficVO('4028805f88c2982d0188c2ec3a4b03b5', '二号出入口', 0, 0, null, null, null));
        list.add(new EntranceTrafficVO('4028805f88c2982d0188c2ecff5f03c1', '三号出入口', 0, 0, null, null, null));

        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd');
        Map<String, Map<String, int[]>> map = new HashMap<>();
        for (EntranceTrafficVO vo : list) {
            if (vo.getYear() == null || vo.getMonth() == null || vo.getDay() == null) {
                continue;
            }
            String dateStr = vo.getYear() + '-' + vo.getMonth() + '-' + vo.getDay();
            Map<String, int[]> idMap = map.get(dateStr);
            if (idMap == null) {
                idMap = new HashMap<>();
                for (EntranceTrafficVO vo2 : list) {
                    idMap.put(vo2.getId(), new int[2]);
                }
                map.put(dateStr, idMap);
            }
            int[] count = idMap.get(vo.getId());
            count[0] += vo.getRksl();
            count[1] += vo.getCksl();
        }

        Set<String> dateSet = new TreeSet<>(map.keySet());
        int[][] result = new int[dateSet.size() + 1][list.size() * 2 + 1];
        result[0][0] = -1; // 表示是日期行
        int i = 1;
        for (String dateStr : dateSet) {
            result[i][0] = i;
            result[i][1] = sdf.parse(dateStr).getTime();
            Map<String, int[]> idMap = map.get(dateStr);
            int j = 2;
            for (EntranceTrafficVO vo : list) {
                int[] count = idMap.get(vo.getId());
                result[i][j++] = count[0];
                result[i][j++] = count[1];
            }
            i++;
        }

        System.out.println(Arrays.deepToString(result));
    }

    private static class EntranceTrafficVO {
        private String id;
        private String entranceName;
        private int rksl;
        private int cksl;
        private Integer year;
        private Integer month;
        private Integer day;

        public EntranceTrafficVO(String id, String entranceName, int rksl, int cksl, Integer year, Integer month, Integer day) {
            this.id = id;
            this.entranceName = entranceName;
            this.rksl = rksl;
            this.cksl = cksl;
            this.year = year;
            this.month = month;
            this.day = day;
        }

        // 省略 getter 和 setter 方法

        @Override
        public String toString() {
            return 'EntranceTrafficVO{' +
                    'id=' + id + ''' +
                    ', entranceName=' + entranceName + ''' +
                    ', rksl=' + rksl +
                    ', cksl=' + cksl +
                    ', year=' + year +
                    ', month=' + month +
                    ', day=' + day +
                    '}';
        }
    }
}

代码说明:

  • 使用 HashMap 来存储日期和 ID 对应的 rkslcksl
  • 使用 TreeSet 来排序日期,以确保输出结果按日期顺序排列。
  • 对于没有数据的日期,对应的 rkslcksl 均为 0。
  • 最后的二维数组包含了所有日期和每个 ID 的 rkslcksl

注意事项:

  • kssjjssj 需要根据实际情况进行定义。
  • 可以根据需要对代码进行调整,例如添加异常处理、优化代码逻辑等。

总结:

本文介绍了使用 Java 集合来统计指定日期范围内每个 ID 的 rkslcksl 的方法。代码示例展示了如何使用 HashMapTreeSet 来高效地进行数据处理,并能够在数据缺失的情况下将缺失的日期统计为 0。

Java 集合统计:获取指定日期范围内每个 ID 的 rksl 和 cksl

原文地址: https://www.cveoy.top/t/topic/oZN7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录