List of usage examples for java.util SortedMap subMap
SortedMap<K, V> subMap(K fromKey, K toKey);
From source file:Main.java
public static void main(String[] args) { NavigableMap<Integer, String> nmap = new TreeMap<Integer, String>(); nmap.put(1, "this"); nmap.put(2, "is"); nmap.put(3, "a"); nmap.put(4, "test"); nmap.put(5, "."); System.out.println(nmap);/*ww w . j av a 2 s .co m*/ System.out.println(nmap.subMap(4, true, 5, true).values()); nmap.subMap(1, true, 3, true).clear(); System.out.println(nmap); // wrap into synchronized SortedMap SortedMap<Integer, String> ssmap = Collections.synchronizedSortedMap(nmap); System.out.println(ssmap.subMap(4, 5)); System.out.println(ssmap.subMap(4, 5 + 1)); }
From source file:Main.java
public static void main(String[] args) { SortedMap<String, String> sMap = new TreeMap<>(); sMap.put("CSS", "style"); sMap.put("HTML", "mark up"); sMap.put("Oracle", "database"); sMap.put("XML", "data"); SortedMap<String, String> subMap = sMap.subMap("CSS", "XML"); System.out.println(subMap);//from w w w.j ava2s .c o m // Get the first and last keys String firstKey = sMap.firstKey(); String lastKey = sMap.lastKey(); System.out.println("First Key: " + firstKey); System.out.println("Last key: " + lastKey); }
From source file:Main.java
public static void main(String[] args) { Comparator<String> keyComparator = Comparator.comparing(String::length) .thenComparing(String::compareToIgnoreCase); SortedMap<String, String> sMap = new TreeMap<>(keyComparator); sMap.put("CSS", "style"); sMap.put("HTML", "mark up"); sMap.put("Oracle", "database"); sMap.put("XML", "data"); SortedMap<String, String> subMap = sMap.subMap("CSS", "XML"); System.out.println(subMap);/*from www .ja v a 2 s . co m*/ // Get the first and last keys String firstKey = sMap.firstKey(); String lastKey = sMap.lastKey(); System.out.println("First Key: " + firstKey); System.out.println("Last key: " + lastKey); }
From source file:Main.java
public static void main(String[] args) { SortedMap<String, Integer> sortedMap = new TreeMap<String, Integer>(); sortedMap.put("A", 1); sortedMap.put("B", 2); sortedMap.put("C", 3); sortedMap.put("D", 4); sortedMap.put("E", 5); sortedMap.put("java2s", 6); SortedMap<String, Integer> map = sortedMap.subMap("B", "E"); System.out.println(map);//from www . j a v a2 s.c om }
From source file:hudson.Functions.java
/** * Creates a sub map by using the given range (both ends inclusive). *///w w w . ja v a2 s . c o m public static <V> SortedMap<Integer, V> filter(SortedMap<Integer, V> map, String from, String to) { if (from == null && to == null) return map; if (to == null) return map.headMap(Integer.parseInt(from) - 1); if (from == null) return map.tailMap(Integer.parseInt(to)); return map.subMap(Integer.parseInt(to), Integer.parseInt(from) - 1); }
From source file:org.jasig.schedassist.web.VisibleScheduleTag.java
@Override public int doStartTagInternal() { final ServletContext servletContext = pageContext.getServletContext(); final Date scheduleStart = visibleSchedule.getScheduleStart(); if (null == scheduleStart) { // the visibleSchedule is empty, short circuit try {/*from ww w .j a va 2 s . c o m*/ StringBuilder noappointments = new StringBuilder(); noappointments.append("<span class=\"none-available\">"); noappointments.append(getMessageSource().getMessage("no.available.appointments", null, null)); noappointments.append("</span>"); pageContext.getOut().write(noappointments.toString()); } catch (IOException e) { LOG.error("IOException occurred in doStartTag", e); } // SKIP_BODY means don't print any content from body of tag return SKIP_BODY; } LOG.debug("scheduleStart: " + scheduleStart); SortedMap<Date, List<AvailableBlock>> dailySchedules = new TreeMap<Date, List<AvailableBlock>>(); Date index = DateUtils.truncate(scheduleStart, java.util.Calendar.DATE); Date scheduleEnd = visibleSchedule.getScheduleEnd(); while (index.before(scheduleEnd)) { dailySchedules.put(index, new ArrayList<AvailableBlock>()); index = DateUtils.addDays(index, 1); } final Date lastMapKey = dailySchedules.lastKey(); LOG.debug("visibleSchedule spans " + dailySchedules.keySet().size() + " days"); try { SortedMap<AvailableBlock, AvailableStatus> scheduleBlockMap = visibleSchedule.getBlockMap(); int numberOfEventsToDisplay = 0; for (AvailableBlock block : scheduleBlockMap.keySet()) { Date eventStartDate = block.getStartTime(); LOG.debug("event start date: " + eventStartDate); Date mapKey = DateUtils.truncate(eventStartDate, java.util.Calendar.DATE); if (CommonDateOperations.equalsOrAfter(eventStartDate, scheduleStart) && dailySchedules.containsKey(mapKey)) { dailySchedules.get(mapKey).add(block); numberOfEventsToDisplay++; } } LOG.debug("number of events to display: " + numberOfEventsToDisplay); if (numberOfEventsToDisplay == 0) { // no available times in this range! StringBuilder noappointments = new StringBuilder(); noappointments.append("<span class=\"none-available\">"); noappointments.append(getMessageSource().getMessage("no.available.appointments", null, null)); noappointments.append("</span>"); pageContext.getOut().write(noappointments.toString()); } else { int weekNumber = 1; Date currentWeekStart = DateUtils.truncate(scheduleStart, java.util.Calendar.DATE); Date currentWeekFinish = DateUtils.addDays(currentWeekStart, CommonDateOperations.numberOfDaysUntilSunday(currentWeekStart)); currentWeekFinish = DateUtils.addMinutes(currentWeekFinish, -1); boolean renderAnotherWeek = true; while (renderAnotherWeek) { if (LOG.isDebugEnabled()) { LOG.debug("will render another week using currentWeekStart " + currentWeekStart + " and currentWeekFinish " + currentWeekFinish); } SortedMap<Date, List<AvailableBlock>> subMap = dailySchedules.subMap(currentWeekStart, currentWeekFinish); renderWeek(servletContext, pageContext.getOut(), weekNumber++, subMap, scheduleBlockMap); currentWeekStart = DateUtils.addMinutes(currentWeekFinish, 1); currentWeekFinish = DateUtils.addDays(currentWeekStart, 7); currentWeekFinish = DateUtils.addMinutes(currentWeekFinish, -1); if (LOG.isDebugEnabled()) { LOG.debug("recalculated currentWeekStart " + currentWeekStart + ", currentWeekFinish " + currentWeekFinish); } if (currentWeekStart.after(lastMapKey)) { renderAnotherWeek = false; LOG.debug("will not render another week"); } } } } catch (IOException e) { LOG.error("IOException occurred in doStartTag", e); } // SKIP_BODY means don't print any content from body of tag return SKIP_BODY; }
From source file:org.jasig.schedassist.portlet.VisibleScheduleTag.java
@Override public int doStartTagInternal() { RenderRequest renderRequest = (RenderRequest) pageContext.getRequest().getAttribute(PORTLET_REQUEST); RenderResponse renderResponse = (RenderResponse) pageContext.getRequest().getAttribute(PORTLET_RESPONSE); final Date scheduleStart = visibleSchedule.getScheduleStart(); if (null == scheduleStart) { // the visibleSchedule is empty, short circuit try {//from ww w . j a va 2s . c o m StringBuilder noappointments = new StringBuilder(); noappointments.append("<span class=\"none-available\">"); noappointments.append(getMessageSource().getMessage("no.available.appointments", null, null)); noappointments.append("</span>"); pageContext.getOut().write(noappointments.toString()); } catch (IOException e) { LOG.error("IOException occurred in doStartTag", e); } // SKIP_BODY means don't print any content from body of tag return SKIP_BODY; } LOG.debug("scheduleStart: " + scheduleStart); SortedMap<Date, List<AvailableBlock>> dailySchedules = new TreeMap<Date, List<AvailableBlock>>(); Date index = DateUtils.truncate(scheduleStart, java.util.Calendar.DATE); Date scheduleEnd = visibleSchedule.getScheduleEnd(); while (index.before(scheduleEnd)) { dailySchedules.put(index, new ArrayList<AvailableBlock>()); index = DateUtils.addDays(index, 1); } final Date lastMapKey = dailySchedules.lastKey(); LOG.debug("visibleSchedule spans " + dailySchedules.keySet().size() + " days"); try { SortedMap<AvailableBlock, AvailableStatus> scheduleBlockMap = visibleSchedule.getBlockMap(); int numberOfEventsToDisplay = 0; for (AvailableBlock block : scheduleBlockMap.keySet()) { Date eventStartDate = block.getStartTime(); LOG.debug("event start date: " + eventStartDate); Date mapKey = DateUtils.truncate(eventStartDate, java.util.Calendar.DATE); if (CommonDateOperations.equalsOrAfter(eventStartDate, scheduleStart) && dailySchedules.containsKey(mapKey)) { dailySchedules.get(mapKey).add(block); numberOfEventsToDisplay++; } } LOG.debug("number of events to display: " + numberOfEventsToDisplay); if (numberOfEventsToDisplay == 0) { // no available times in this range! StringBuilder noappointments = new StringBuilder(); noappointments.append("<span class=\"none-available\">"); noappointments.append(getMessageSource().getMessage("no.available.appointments", null, null)); noappointments.append("</span>"); pageContext.getOut().write(noappointments.toString()); } else { int weekNumber = 1; Date currentWeekStart = DateUtils.truncate(scheduleStart, java.util.Calendar.DATE); Date currentWeekFinish = DateUtils.addDays(currentWeekStart, CommonDateOperations.numberOfDaysUntilSunday(currentWeekStart)); currentWeekFinish = DateUtils.addMinutes(currentWeekFinish, -1); boolean renderAnotherWeek = true; while (renderAnotherWeek) { if (LOG.isDebugEnabled()) { LOG.debug("will render another week using currentWeekStart " + currentWeekStart + " and currentWeekFinish " + currentWeekFinish); } SortedMap<Date, List<AvailableBlock>> subMap = dailySchedules.subMap(currentWeekStart, currentWeekFinish); renderWeek(pageContext.getOut(), weekNumber++, subMap, scheduleBlockMap, renderRequest, renderResponse); currentWeekStart = DateUtils.addMinutes(currentWeekFinish, 1); currentWeekFinish = DateUtils.addDays(currentWeekStart, 7); currentWeekFinish = DateUtils.addMinutes(currentWeekFinish, -1); if (LOG.isDebugEnabled()) { LOG.debug("recalculated currentWeekStart " + currentWeekStart + ", currentWeekFinish " + currentWeekFinish); } if (currentWeekStart.after(lastMapKey)) { renderAnotherWeek = false; LOG.debug("will not render another week"); } } } } catch (IOException e) { LOG.error("IOException occurred in doStartTag", e); } // SKIP_BODY means don't print any content from body of tag return SKIP_BODY; }
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
/** * Test method for 'java.util.SortedMap.subMap(Object, Object)'. * * @see java.util.SortedMap#subMap(Object, Object) *///from ww w. j a v a2 s. co m public void testSubMap_throwsIllegalArgumentException() { SortedMap<K, V> sortedMap = createNavigableMap(); try { sortedMap.subMap(getGreaterThanMaximumKey(), getLessThanMinimumKey()); fail("expected exception"); } catch (IllegalArgumentException e) { // from key is greater than the to key // expected outcome } }
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
/** * Test method for 'java.util.SortedMap.subMap(Object, Object)'. * * @see java.util.SortedMap#subMap(Object, Object) *//*from w ww. j a v a 2s .co m*/ public void testSubMap_throwsNullPointerException() { SortedMap<K, V> sortedMap = createNavigableMap(); try { sortedMap.subMap(null, getLessThanMinimumKey()); assertTrue(useNullKey()); } catch (NullPointerException e) { assertFalse(useNullKey()); } try { sortedMap.subMap(null, getGreaterThanMaximumKey()); assertTrue(useNullKey()); } catch (NullPointerException e) { assertFalse(useNullKey()); } }
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
/** * Test method for 'java.util.SortedMap.subMap(Object, Object)'. * * @see java.util.SortedMap#subMap(Object, Object) *//*from www . j a v a 2s . co m*/ @SuppressWarnings("unchecked") public void testSubMap_throwsClassCastException() { K[] keys = getKeys(); V[] values = getValues(); SortedMap sortedMap = createNavigableMap(); try { sortedMap.subMap(getConflictingKey(), keys[0]); fail("ClassCastException expected"); } catch (ClassCastException expected) { } try { sortedMap.subMap(keys[0], getConflictingKey()); fail("ClassCastException expected"); } catch (ClassCastException expected) { } sortedMap.put(keys[0], values[0]); try { sortedMap.subMap(getConflictingKey(), keys[0]); fail("ClassCastException expected"); } catch (ClassCastException expected) { } try { sortedMap.subMap(keys[0], getConflictingKey()); fail("ClassCastException expected"); } catch (ClassCastException expected) { } }