Java:我如何扫描地图?

时间:2016-09-26 09:58:21

标签: java hashmap

这是我的控制者:

public class AccessLogController extends ControllerServlet {

private static final long serialVersionUID = 1L;

public void buildAccessLogPaginationView() {

    HttpServletRequest req = getThreadLocalRequest();
    AccessLogRepository accessLogRepo = FactoryDAO.getFactory().getAccessLogRepository(getHibernateSession());

    String resultForPageSelect = "10";

    if(StringUtils.trimAndNullIfEmpty(req.getParameter("resultForPageSelect")) != null)
        resultForPageSelect = req.getParameter("resultForPageSelect");


    AccessLogPaginationView accessLogPaginationView = new AccessLogPaginationView();
    Integer page = new Integer(1);
    if (req.getParameter("page") != null)
        page = new Integer(req.getParameter("page"));
    accessLogPaginationView.setPage(page);
    StringBuffer urlPagination = new StringBuffer();
    urlPagination.append("/accessLog/accessLogController?serviceName=buildAccessLogPaginationView");
    UserIntermediary userIntermediary = getUserInformation();
    if (userIntermediary != null) {

        try {

            List<Date> loginTimes = accessLogRepo.getLoginTime();

            if(loginTimes.size() > 0){
                Map<Integer, Date> datesMap = new TreeMap<Integer, Date>();
                Calendar date = Calendar.getInstance();
                         date.setTime(loginTimes.get(0));
                         date.set(Calendar.HOUR_OF_DAY, 0);
                         date.set(Calendar.MINUTE, 0);
                         date.set(Calendar.SECOND, 0);
                datesMap.put(0, date.getTime());

                int key = 1;
                for (int i = 1; i < loginTimes.size(); i++) {
                    Calendar nextDate = Calendar.getInstance();
                             nextDate.setTime(loginTimes.get(i));
                             nextDate.set(Calendar.HOUR_OF_DAY, 0);
                             nextDate.set(Calendar.MINUTE, 0);
                             nextDate.set(Calendar.SECOND, 0);
                    if (nextDate.getTime().compareTo(datesMap.get(key - 1)) != 0) {
                        datesMap.put(key, nextDate.getTime());
                        key++;
                    }
                }

                for (int j = 0; j < datesMap.size(); j++) {
                    Date minTime = datesMap.get(j);

                    Calendar maxTime = Calendar.getInstance();
                             maxTime.setTime(minTime);
                             maxTime.set(Calendar.HOUR_OF_DAY, 23);
                             maxTime.set(Calendar.MINUTE, 59);
                             maxTime.set(Calendar.SECOND, 59);


                    @SuppressWarnings("unchecked")

                    List<AccessLog> accessesLog = accessLogRepo.getByPeriod(minTime, maxTime.getTime(),((page.intValue() - 1) * Integer.parseInt(resultForPageSelect)), Integer.parseInt(resultForPageSelect));
                    List<Long> accessesTimes = new ArrayList<Long>();
                    for (AccessLog accessLog : accessesLog) {
                        if(accessLog.getLogoutTime() != null){
                            long accessTime = accessLog.getLogoutTime().getTime() - accessLog.getLoginTime().getTime();
                            accessesTimes.add(accessTime / 1000 % 60);
                        }
                    }

                    Long maxAccessTime = new Long(0);
                    for (Long time : accessesTimes) {
                        if (time > maxAccessTime) {
                            maxAccessTime = time;
                        }

                    }
                    DailyAccessLogView dailyAccessLogView = accessLogPaginationView.new DailyAccessLogView(); 
                    dailyAccessLogView.setDate(minTime);
                    dailyAccessLogView.setAccessesNumber(new Long(accessesLog.size()));
                    dailyAccessLogView.setMaxAccessTime( accessesLog.size() > 0 ? (maxAccessTime + 1) : 0 );

                    if(!dailyAccessLogView.getAccessesNumber().equals(new Long(0))){
                        if(accessLogPaginationView.getDailyAccessesLogView().size() < Integer.parseInt(resultForPageSelect)){
                            accessLogPaginationView.getDailyAccessesLogView().add(dailyAccessLogView);
                    }
                }
                }
                Integer count = datesMap.size();
                PaginationView paginationView = PaginationView.paginate(count, Integer.parseInt(resultForPageSelect), urlPagination);
                accessLogPaginationView.setPaginationView(paginationView);
                req.setAttribute(RequestAttributeKeys.VIEW_KEY, accessLogPaginationView);
            }



            dispatchForward("accessLogPagination.tiles");

        } catch (Throwable t) {
            throw new RuntimeException(t);
        }
    } else {
        // user not logged
        dispatchForward("/jsp/login/login.jsp");
    }
}

我的问题是:如何使用变量 resultForPageSelect 扫描我的dateMap Map。

例如:如果第一页的结果是20,则地图必须可视化地图的前20个元素。如果我更改了页面的编号(也就是说,我转到下一页),在第二页上我必须可视化20到40之间的元素。有人可以帮我吗?谢谢。

1 个答案:

答案 0 :(得分:2)

您可以使用的是NavigableMap#subMap(K fromKey, K toKey)(已经可以从您的TreeMap获取,因为它已经是NavigableMap),因为您的密钥是从{{1}开始的结果索引}}

0
  

NavigableMap<Integer, Date> datesMap = new TreeMap<>(); ... // Get only the 20 first entries Map<Integer, Date> subMap = datesMap.subMap(0, 20); ... // Get only the next 20 entries Map<Integer, Date> nextSubMap = datesMap.subMap(20, 40); 返回此地图部分的视图,其范围为   subMap(K fromKey, K toKey),包括fromKey,独占