簡體   English   中英

Java 中的嵌套映射

[英]Nested Mapping in Java

我從數據庫中獲取了以下數據。

Date                Name        users
-----               -------     ------
2016-11-23 00:00:00 aaaa        11
2016-11-24 00:00:00 aaaa        12
2016-11-25 00:00:00 aaaa        14
2016-11-24 00:00:00 bbbb        21
2016-11-26 00:00:00 bbbb        22
2016-11-23 00:00:00 cccc        31
2016-11-25 00:00:00 cccc        32

數據是使用 GoogleAnalyticsPropertyDTO 從 postgresql 中獲取的

我需要將它移動到一個 ListSeries(大小為 11 以保存從 11 月 20 日到 11 月 30 日的每一天的數據),這樣

預期結果:

result[0] = new ListSeries("aaa"    , new Number[] { 0,0,0,11,12,14,0,0,0,0,0});
result[1] = new ListSeries("bbb"    , new Number[] { 0,0,0,0,21,22,0,0,0,0,0 });
result[2] = new ListSeries("ccc"    , new Number[] { 0,0,0,31,0,32,0,0,0,0,0 });

我試過Map<Date, GoogleAnalyticsPropertyDTO> map = new HashMap<Date, GoogleAnalyticsPropertyDTO>(); 但只獲取每個日期最后一個系列的用戶價值。 有人可以幫助我使用 Date 和 Name 值進行映射,以便為我預期的列表系列進行映射嗎? MultiMap 可以用於此嗎?

我試過的代碼

private Number[] getData(Date fromDate, Date toDate) {
        Date from = DateUtility.getDateWithoutTime(fromDate.getTime());
        Date to = DateUtility.getDateWithoutTime(toDate.getTime());
        Number[] result;
        int days = DateUtility.getDaysBetweenDates(from, to);
        result = new Number[days];

        List<GoogleAnalyticsPropertyDTO> gaPropList = XXXXXUI.getBusinessService().getGAProperty(fromDate, toDate);
        Map<Date, GoogleAnalyticsPropertyDTO> map = new HashMap<Date, GoogleAnalyticsPropertyDTO>();
        if (gaPropList != null) {
            for (GoogleAnalyticsPropertyDTO gaProperty : gaPropList) {
                Date dateString = gaProperty.getFromDate();
                map.put(dateString, gaProperty);
            }
            Date date;
            for (int d = 0; d < days; d++) {
                date = DateUtility.addNDaysToDate(from, d);
                if (null == map.get(date)) {
                    result[d] = 0;
                } else {
                    GoogleAnalyticsPropertyDTO a = (GoogleAnalyticsPropertyDTO) map.get(date);
                   if (null == a.getUsers()) {
                       result[d] = 0;
                   } else {
                        result[d] = a.getUsers();
                  }
                }
            }
        }   
        return result;
    }

我不相信你的地圖定義會讓你做你期望的事情。 聽起來您想將表中的行映射到日期字符串。 然后你將擁有的價值是:

Map<String, Collection<GoogleAnalyticsPropertyDTO>>

而且,當您遍歷所有這些時,您可以像這樣按日期映射:

map.get(dateString).add(gaProperty);

暫無
暫無

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

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