簡體   English   中英

如何使用 hashmap 的<integer,hashmap<integer,integer></integer,hashmap<integer,integer>

[英]How to add increment values using a hashmap of <Integer,HashMap<Integer,Integer>

我有一個名為 client 的表,其中有一個名為 created_time 的列,所以實際上我想 plot 一個 map,這樣我就可以知道在哪一年和哪一個月添加了多少客戶? 現在的問題是假設在 2018 年 11 月添加了 2 個客戶,在 2018 年 12 月添加了 0 個客戶,所以在 12 月我也會顯示 2 個(就像上面的月份 + 當前月份),如果在 2019 年 1 月添加了 1 個客戶,那么我會展示 3 等等

所以為了解決這個問題,我創建了一個 hashMap,其中第一個鍵是年,我創建了另一個 hashMap,其中鍵是月,然后是添加的客戶端編號,例如 HashMap>

預期 Output::

clientsByMonth": {
                    "2018": {
                        "10": 1,(1 client added in oct)
11,1( as 0 client added in nov)
12,2( as 1 client added in december)
                    },
                    "2019": {
                        "1": 2,   ( as no client added )
                        "2": 4,   (as 2 client added)
                        "3": 5,
                        "4": 5
                    }
    HashMap<Integer, HashMap<Integer, Integer>> clientsByYear = new HashMap<>();
    List<Client> clientList = clientRepository.findAll();

    for (int i = 0; i < clientList.size(); i++) {

        Instant instant = Instant.ofEpochMilli(clientList.get(i).getCreatedTime().getTime());
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        Integer month = localDateTime.toLocalDate().getMonth().getValue();
        Integer year = localDateTime.toLocalDate().getYear();

        if (!clientsByYear.containsKey(year)) {
            HashMap<Integer, Integer> clientByMonth = new HashMap<>();
            clientByMonth.put(month, 1);
            clientsByYear.put(year, clientByMonth);
        } else {
            HashMap<Integer, Integer> clientByMonth = clientsByYear.get(year);
            if (!clientByMonth.containsKey(month)) {
                int clients = 0;
                for (int j = month - 1; j >= 0; j--) {
                    if (clientByMonth.containsKey(j)) {
                        clients = clients + clientByMonth.get(j);
                        break;
                    }
                }
                clientByMonth.put(month, (clients + 1));
            } else if (clientByMonth.containsKey(month)) {
                int clients = clientByMonth.get(month);
                clientByMonth.put(month, (clients + 1));
            }
        }
    }

見下文,如果我正確理解您的問題,它應該可以解決您的問題

  1. 迭代所有客戶端
  2. 如果年份 map 不包含它,則添加
  3. 否則從 yearmap 獲取現有條目
  4. 添加月份 map,迭代計數器為最新值
for (int i = 1; i <=clientList.size() ; i++) {
        ...

     if (!clientsByYear.containsKey(year)) {
            HashMap<Integer, Integer> clientByMonth = new HashMap<>();
            clientByMonth.put(month, i);
            clientsByYear.put(year, clientByMonth);
        } else {
            HashMap<Integer, Integer> clientByMonth = clientsByYear.get(year);
            clientByMonth.put(month, i);
        }
      }
    System.out.println(clientsByYear);
  }

預期:輸入數據按時間 asc 排序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM