Java Code Examples for java.util.TreeSet#tailSet()

The following examples show how to use java.util.TreeSet#tailSet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WildcardPattern.java    From pumpernickel with MIT License 6 votes vote down vote up
/**
 * Return all the elements of the argument that comply with this pattern.
 * <p>
 * This should be functionally equivalent to iterating over every element of
 * the set and checking its compliance, but depending on the composition of
 * this pattern: this method may be faster than if you iterate over all
 * elements directly.
 * 
 * @param set
 * @param string
 * @return
 */
public SortedSet<String> getMatches(TreeSet<String> set) {
	SortedSet<String> returnValue = new TreeSet<>();
	Placeholder[] phs = getPlaceholders();

	StringBuilder constantPrefixBuilder = new StringBuilder();
	for (int a = 0; a < phs.length; a++) {
		if (phs[a] instanceof FixedCharacter) {
			FixedCharacter fc = (FixedCharacter) phs[a];
			constantPrefixBuilder.append(fc.ch);
		} else {
			break;
		}
	}
	String constantPrefix = constantPrefixBuilder.toString();

	for (String s : set.tailSet(constantPrefix, true)) {
		if (matches(s))
			returnValue.add(s);
		if (!s.startsWith(constantPrefix))
			break;
	}

	return returnValue;
}
 
Example 2
Source File: Scheduler.java    From sagetv with Apache License 2.0 6 votes vote down vote up
/**
 * Coalate elements that overlap the given interval and return as a linked list.
 */
static <T extends LongInterval> LinkedList<T> getOverlaps(TreeSet<T> set, T interval) {
  LinkedList<T> list = new LinkedList<T>();
  SortedSet<T> subSet = set.headSet(interval);

  // There is only ever one element below the window that can overlap.
  if (!subSet.isEmpty()) {
    T last = subSet.last();
    if (last.getUpper() > interval.getLower()) {
      list.addFirst(last);
    }
  }

  // There are multiple elements that can be above the window start since the set is sorted by
  // just the lower value.
  final long upperLimit = interval.getUpper();
  for (T tmp : set.tailSet(interval)) { // Java 1.5: inclusive of interval
    if (tmp.getLower() < upperLimit) {
      list.add(tmp);
    } else {
      break;
    }
  }
  return list;
}
 
Example 3
Source File: TreeSetTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * tailSet returns set with keys in requested range
 */
public void testTailSetContents() {
    TreeSet set = set5();
    SortedSet sm = set.tailSet(two);
    assertFalse(sm.contains(one));
    assertTrue(sm.contains(two));
    assertTrue(sm.contains(three));
    assertTrue(sm.contains(four));
    assertTrue(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    k = (Integer)(i.next());
    assertEquals(four, k);
    k = (Integer)(i.next());
    assertEquals(five, k);
    assertFalse(i.hasNext());

    SortedSet ssm = sm.tailSet(four);
    assertEquals(four, ssm.first());
    assertEquals(five, ssm.last());
    assertTrue(ssm.remove(four));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, set.size());
}
 
Example 4
Source File: Scheduler.java    From sagetv with Apache License 2.0 5 votes vote down vote up
/**
 * Test if the given interval overlaps any element in the set
 * @param set of elements
 * @param interval to test for overlap
 * @return true if overlaps, false otherwise
 */
static <T extends LongInterval> boolean hasOverlapInSet(TreeSet<T> set, T interval) {
  SortedSet<T> subSet = set.headSet(interval);
  if (!subSet.isEmpty() && subSet.last().getUpper() > interval.getLower()) return true;
  subSet = set.tailSet(interval); // Java 1.5: inclusive of interval
  if (!subSet.isEmpty() && subSet.first().getLower() < interval.getUpper()) return true;
  return false;
}
 
Example 5
Source File: TreeSetTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * tailSet returns set with keys in requested range
 */
public void testTailSetContents() {
    TreeSet set = set5();
    SortedSet sm = set.tailSet(two);
    assertFalse(sm.contains(one));
    assertTrue(sm.contains(two));
    assertTrue(sm.contains(three));
    assertTrue(sm.contains(four));
    assertTrue(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    k = (Integer)(i.next());
    assertEquals(four, k);
    k = (Integer)(i.next());
    assertEquals(five, k);
    assertFalse(i.hasNext());

    SortedSet ssm = sm.tailSet(four);
    assertEquals(four, ssm.first());
    assertEquals(five, ssm.last());
    assertTrue(ssm.remove(four));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, set.size());
}
 
Example 6
Source File: ZipStructure.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * In this method we'll get the contents within the zip file for the passed directory
 *
 * @param string: Must be a directory within the zip file or an empty string to get the root contents
 */
public List<String> contents(String name) {
    ArrayList<String> ret = new ArrayList<String>();

    int level;
    int length = name.length();
    if (length == 0) {
        level = 0;
    } else {
        Assert.isTrue(StringUtils.endsWith(name, '/')); //must be a directory
        level = StringUtils.count(name, '/');
    }

    TreeSet<String> treeSet = levelToContents.get(level);
    if (treeSet != null) {
        if (length == 0) {
            ret.addAll(treeSet);
        } else {
            for (String s : treeSet.tailSet(name)) {
                if (s.startsWith(name)) {
                    ret.add(s);
                }
            }
        }
    }

    return ret;
}
 
Example 7
Source File: TreeSubSetTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static NavigableSet set0() {
    TreeSet set = new TreeSet();
    assertTrue(set.isEmpty());
    return set.tailSet(m1, false);
}
 
Example 8
Source File: TreeSubSetTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static NavigableSet set0() {
    TreeSet set = new TreeSet();
    assertTrue(set.isEmpty());
    return set.tailSet(m1, false);
}
 
Example 9
Source File: RedissonSortedSetTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testTailSetTreeSet() {
    TreeSet<Integer> set = new TreeSet<Integer>();

    set.add(1);
    set.add(2);
    set.add(3);
    set.add(4);
    set.add(5);

    SortedSet<Integer> hs = set.tailSet(3);
    hs.add(10);

    assertThat(hs).containsExactly(3, 4, 5, 10);

    set.remove(4);

    assertThat(hs).containsExactly(3, 5, 10);

    set.remove(3);

    assertThat(hs).containsExactly(5, 10);

    hs.add(-1);
}
 
Example 10
Source File: RedissonScoredSortedSetTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testTailSetTreeSet() {
    TreeSet<Integer> set = new TreeSet<Integer>();

    set.add(1);
    set.add(2);
    set.add(3);
    set.add(4);
    set.add(5);

    SortedSet<Integer> hs = set.tailSet(3);
    hs.add(10);

    assertThat(hs).containsExactly(3, 4, 5, 10);

    set.remove(4);

    assertThat(hs).containsExactly(3, 5, 10);

    set.remove(3);

    assertThat(hs).containsExactly(5, 10);

    hs.add(-1);
}
 
Example 11
Source File: FailedBuildHistoryCacheSweeper.java    From gocd with Apache License 2.0 4 votes vote down vote up
@Override
public void added(PipelineTimelineEntry newlyAddedEntry, TreeSet<PipelineTimelineEntry> timeline) {
    for (PipelineTimelineEntry pipelineTimelineEntry : timeline.tailSet(newlyAddedEntry)) {
        goCache.remove(key.forFbhOfStagesUnderPipeline(pipelineTimelineEntry.getPipelineLocator()));
    }
}