Example usage for java.util SortedSet tailSet

List of usage examples for java.util SortedSet tailSet

Introduction

In this page you can find the example usage for java.util SortedSet tailSet.

Prototype

SortedSet<E> tailSet(E fromElement);

Source Link

Document

Returns a view of the portion of this set whose elements are greater than or equal to fromElement .

Usage

From source file:Tail.java

public static void main(String args[]) throws Exception {
    String elements[] = { "Irish Setter", "Poodle", "English Setter", "Gordon Setter", "Pug" };
    SortedSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set.tailSet("Irish Setter"));
    System.out.println(set.headSet("Irish Setter"));
    System.out.println(set.headSet("Irish Setter\0"));
    System.out.println(set.tailSet("Irish Setter\0"));
    System.out.println(set.subSet("Irish Setter", "Poodle\0"));
    System.out.println(set.subSet("Irish Setter", "Irish Setter\0"));
    System.out.println(set.subSet("Irish Setter", "Irish Setter"));
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    SortedSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set.tailSet("I"));
    System.out.println(set.headSet("I"));
    System.out.println(set.headSet("I\0"));
    System.out.println(set.tailSet("I\0"));
    System.out.println(set.subSet("I", "P\0"));
    System.out.println(set.subSet("I", "I\0"));
    System.out.println(set.subSet("I", "I"));
}

From source file:Main.java

public static void main(String[] args) {
    SortedSet<String> names = new TreeSet<>();
    names.add("HTML");
    names.add("Java");
    names.add("SQL");
    names.add("CSS");
    System.out.println("Sorted Set: " + names);
    System.out.println("First: " + names.first());
    System.out.println("Last: " + names.last());

    SortedSet<String> ssBeforeCSS = names.headSet("CSS");
    System.out.println(ssBeforeCSS);

    SortedSet<String> ssBetwenCSSAndHTML = names.subSet("CSS", "HTML");
    System.out.println(ssBetwenCSSAndHTML);

    SortedSet<String> ssBetwenCSSAndHTML2 = names.subSet("CSS", "HTML");
    System.out.println(ssBetwenCSSAndHTML2);

    SortedSet<String> ssCSSAndAfter = names.tailSet("CSS");
    System.out.println(ssCSSAndAfter);

}

From source file:SortedSetDemo.java

public static void main(String[] args) {
    SortedSet sortedSet = new TreeSet(Arrays.asList("one two three four five six seven eight".split(" ")));
    System.out.println(sortedSet);
    Object low = sortedSet.first(), high = sortedSet.last();
    System.out.println(low);//from www  . j  a v a2s  .  co m
    System.out.println(high);
    Iterator it = sortedSet.iterator();
    for (int i = 0; i <= 6; i++) {
        if (i == 3)
            low = it.next();
        if (i == 6)
            high = it.next();
        else
            it.next();
    }
    System.out.println(low);
    System.out.println(high);
    System.out.println(sortedSet.subSet(low, high));
    System.out.println(sortedSet.headSet(high));
    System.out.println(sortedSet.tailSet(low));
}

From source file:com.espertech.esper.schedule.ScheduleComputeHelper.java

private static int nextValue(SortedSet<Integer> valueSet, int startValue) {
    if (valueSet == null) {
        return startValue;
    }//  ww  w .  j a  v a2s . c om

    if (valueSet.contains(startValue)) {
        return startValue;
    }

    SortedSet<Integer> tailSet = valueSet.tailSet(startValue + 1);

    if (tailSet.isEmpty()) {
        return -1;
    } else {
        return tailSet.first();
    }
}

From source file:com.cyberway.issue.net.PublicSuffixes.java

protected static void buildRegex(String stem, StringBuilder regex, SortedSet<String> prefixes) {
    if (prefixes.isEmpty()) {
        return;/*  w  ww.j a va 2 s . c  o m*/
    }
    if (prefixes.size() == 1 && prefixes.first().equals(stem)) {
        // avoid unnecessary "(?:)"
        return;
    }
    regex.append("(?:");
    if (stem.length() == 0) {
        regex.append("\n "); // linebreak-space before first character
    }
    Iterator<String> iter = prefixes.iterator();
    char c = 0;
    while (iter.hasNext()) {
        String s = iter.next();
        if (s.length() > stem.length()) {
            char d = s.charAt(stem.length());

            if (d == '+') {
                // convert exception to zero-width-positive-lookahead
                regex.append("(?=" + s.substring(stem.length() + 1) + ")");
            } else {
                if (d == c) {
                    continue;
                }
                c = d;
                regex.append(c);
                String newStem = s.substring(0, stem.length() + 1);
                SortedSet<String> tail = prefixes.tailSet(newStem);
                SortedSet<String> range = null;
                successor: for (String candidate : tail) {
                    if (!candidate.equals(newStem)) {
                        range = prefixes.subSet(s, candidate);
                        break successor;
                    }
                }
                if (range == null) {
                    range = prefixes.tailSet(s);
                }
                buildRegex(newStem, regex, range);
            }
            regex.append('|');
        } else {
            // empty suffix; insert dummy to be eaten when loop exits
            regex.append('@');
        }
    }
    // eat the trailing '|' (if no empty '@') or dummy
    regex.deleteCharAt(regex.length() - 1);
    regex.append(')');
    if (stem.length() == 1) {
        regex.append('\n'); // linebreak for TLDs
    }
}

From source file:com.appeligo.showfiles.FilesByTime.java

/**
 * @param request//from  w  w  w .  ja v  a  2s  .  c  o  m
 * @param out
 * @param path
 */
private void listFiles(HttpServletRequest request, PrintWriter out, String path, int limit) {
    header(out);
    Comparator<File> comparator = new Comparator<File>() {
        public int compare(File leftFile, File rightFile) {
            long leftMod = leftFile.lastModified();
            long rightMod = rightFile.lastModified();
            if (leftMod < rightMod) {
                return -1;
            } else if (leftMod > rightMod) {
                return 1;
            } else {
                return leftFile.getPath().compareTo(rightFile.getPath());
            }
        }
    };
    SortedSet<File> fileSet = new TreeSet<File>(comparator);
    addFile(fileSet, new File(path));

    log.info("Total files in tree is " + fileSet.size());

    if (limit > 0 && fileSet.size() > limit) {
        log.info("Trimming tree to limit " + limit);
        Iterator<File> iter = fileSet.iterator();
        int toDrop = fileSet.size() - limit;
        for (int i = 0; i < toDrop; i++) {
            iter.next();
        }
        File first = iter.next();
        fileSet = fileSet.tailSet(first);
    }

    int suggestedLimit = 1000;
    if (limit == 0 && fileSet.size() > suggestedLimit) {
        out.println("That's a lot of files!  There are " + fileSet.size() + " files to return.<br/>");
        out.println("How about just the <a href=\"" + request.getRequestURI() + "?" + suggestedLimit
                + "\">last " + suggestedLimit + "</a>.<br/>");
        out.println("If you really want them all, <a href=\"" + request.getRequestURI() + "?"
                + (fileSet.size() + suggestedLimit) + "\">click here</a>.<br/>");
    } else {

        DateFormat dateFormat = SimpleDateFormat.getDateInstance();
        DateFormat timeFormat = SimpleDateFormat.getTimeInstance();
        Calendar lastDay = Calendar.getInstance();
        Calendar day = Calendar.getInstance();
        boolean first = true;

        for (File file : fileSet) {
            Date fileDate = new Date(file.lastModified());
            day.setTime(fileDate);
            if (first || lastDay.get(Calendar.DAY_OF_YEAR) != day.get(Calendar.DAY_OF_YEAR)) {
                out.print("<b>" + dateFormat.format(fileDate) + "</b><br/>");
            }
            String servlet = "/ShowFile";
            if (file.getPath().endsWith(".flv")) {
                servlet = "/ShowFlv";
            }
            out.print(timeFormat.format(fileDate) + " <a href=\"" + request.getContextPath() + servlet
                    + file.getPath().substring(documentRoot.length()) + "\">" + file.getPath() + "</a>");
            out.println("<br/>");
            lastDay.setTime(fileDate);
            first = false;
        }
    }
    footer(out);
}

From source file:com.opengamma.integration.coppclark.CoppClarkHolidayFileReader.java

private void mergeDates(HolidayDocument existingDoc, HolidayDocument newDoc) {
    if (newDoc.getHoliday().getHolidayDates().size() == 0) {
        return;/*from  w w w .j  a va 2 s .  c  o  m*/
    }

    // merge dates
    SortedSet<LocalDate> existingDates = new TreeSet<LocalDate>(existingDoc.getHoliday().getHolidayDates());
    SortedSet<LocalDate> newDates = new TreeSet<LocalDate>(newDoc.getHoliday().getHolidayDates());
    List<LocalDate> result = new ArrayList<LocalDate>(newDates);
    result.addAll(0, existingDates.headSet(newDates.first()));
    result.addAll(existingDates.tailSet(newDates.last().plusYears(1).withDayOfYear(1))); // file is based on whole years

    // store into new document
    newDoc.getHoliday().getHolidayDates().clear();
    newDoc.getHoliday().getHolidayDates().addAll(result);
}

From source file:info.rmapproject.core.rmapservice.impl.openrdf.ORMapDiSCOMgr.java

/**
 * Get IRI of next version of a DiSCO.//w  w w. j  a  v  a2 s .co  m
 *
 * @param discoID  IRI of DISCO
 * @param event2disco Map from events to all versions of DiSCOs
 * @param date2event  Map from date events associated with version of DiSCO
 * @param ts the triplestore instance
 * @return IRI of next version of DiSCO, or null if none found
 * @throws RMapException the RMap exception
 * @throws RMapObjectNotFoundException the RMap object not found exception
 * @throws RMapDefectiveArgumentException the RMap defective argument exception
 */
protected IRI getNextIRI(IRI discoID, Map<IRI, IRI> event2disco, Map<Date, IRI> date2event,
        SesameTriplestore ts)
        throws RMapException, RMapObjectNotFoundException, RMapDefectiveArgumentException {
    if (discoID == null) {
        throw new RMapDefectiveArgumentException("null DiSCO id");
    }
    if (event2disco == null) {
        throw new RMapDefectiveArgumentException("Null event2disco map");
    }
    Map<IRI, IRI> disco2event = Utils.invertMap(event2disco);
    if (date2event == null) {
        date2event = eventmgr.getDate2EventMap(event2disco.keySet(), ts);
    }
    Map<IRI, Date> event2date = Utils.invertMap(date2event);
    IRI discoEventId = disco2event.get(discoID);
    Date eventDate = event2date.get(discoEventId);
    SortedSet<Date> sortedDates = new TreeSet<Date>();
    sortedDates.addAll(date2event.keySet());
    SortedSet<Date> laterDates = sortedDates.tailSet(eventDate);
    IRI nextDiscoId = null;
    if (laterDates.size() > 1) {
        Date[] dateArray = laterDates.toArray(new Date[laterDates.size()]);
        IRI nextEventId = date2event.get(dateArray[1]);
        nextDiscoId = event2disco.get(nextEventId);
    }
    return nextDiscoId;
}

From source file:com.yahoo.pulsar.common.naming.NamespaceBundlesTest.java

@Test
public void testFindBundle() throws Exception {
    SortedSet<Long> partitions = Sets.newTreeSet();
    partitions.add(0l);/*from www .  j  a  va2 s.  co  m*/
    partitions.add(0x40000000l);
    partitions.add(0xa0000000l);
    partitions.add(0xb0000000l);
    partitions.add(0xc0000000l);
    partitions.add(0xffffffffl);
    NamespaceBundles bundles = new NamespaceBundles(new NamespaceName("pulsar/global/ns1"), partitions,
            factory);
    DestinationName dn = DestinationName.get("persistent://pulsar/global/ns1/topic-1");
    NamespaceBundle bundle = bundles.findBundle(dn);
    assertTrue(bundle.includes(dn));

    dn = DestinationName.get("persistent://pulsar/use/ns2/topic-2");
    try {
        bundles.findBundle(dn);
        fail("Should have failed due to mismatched namespace name");
    } catch (IllegalArgumentException iae) {
        // OK, expected
    }

    Long hashKey = factory.getLongHashCode(dn.toString());
    // The following code guarantees that we have at least two ranges after the hashKey till the end
    SortedSet<Long> tailSet = partitions.tailSet(hashKey);
    tailSet.add(hashKey);
    // Now, remove the first range to ensure the hashKey is not included in <code>newPar</code>
    Iterator<Long> iter = tailSet.iterator();
    iter.next();
    SortedSet<Long> newPar = tailSet.tailSet(iter.next());

    try {
        bundles = new NamespaceBundles(dn.getNamespaceObject(), newPar, factory);
        bundles.findBundle(dn);
        fail("Should have failed due to out-of-range");
    } catch (ArrayIndexOutOfBoundsException iae) {
        // OK, expected
    }
}