[英]Java: How can i scan a Map?
This is my controller: 这是我的控制器:
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");
}
}
My question is: how can i scan my dateMap Map using the variable resultForPageSelect . 我的问题是:如何使用变量resultForPageSelect扫描我的dateMap地图。
Ex: if the results of the first page are 20, the Map has to visualize the first 20 elements of the map. 例如:如果第一页的结果是20,则地图必须可视化地图的前20个元素。 If i change the number of the page (aka, i go to next page),on the second page i have to visualize the elements from 20 to 40. Someone can help me please?
如果我更改页面的编号(又名,转到下一页),则必须在第二页上将元素从20可视化为40。有人可以帮助我吗? Thank you.
谢谢。
What you can use is NavigableMap#subMap(K fromKey, K toKey)
(that is already available from your TreeMap
as it is already a NavigableMap
) as your key is the index of the results starting from 0
. 您可以使用
NavigableMap#subMap(K fromKey, K toKey)
(因为它已经是NavigableMap
,因此可以从TreeMap
中获得),因为您的键是从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)
: Returns a view of the portion of this map whose keys range fromfromKey
, inclusive, totoKey
, exclusivesubMap(K fromKey, K toKey)
:返回此地图部分的视图,其键范围从fromKey
到toKey
,到toKey
到toKey
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.