Java中的嵌套映射

时间:2016-12-04 05:55:38

标签: java mapping

我从DB中获取了以下数据。

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;
    }

0 个答案:

没有答案