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

The following examples show how to use java.util.TreeSet#headSet() . These examples are extracted from open source projects. 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
/**
 * headSet returns set with keys in requested range
 */
public void testHeadSetContents() {
    TreeSet set = set5();
    SortedSet sm = set.headSet(four);
    assertTrue(sm.contains(one));
    assertTrue(sm.contains(two));
    assertTrue(sm.contains(three));
    assertFalse(sm.contains(four));
    assertFalse(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(one, k);
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, set.size());
    assertEquals(four, set.first());
}
 
Example 2
Source Project: sagetv   File: Scheduler.java    License: 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 Project: j2objc   File: TreeSetTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * headSet returns set with keys in requested range
 */
public void testHeadSetContents() {
    TreeSet set = set5();
    SortedSet sm = set.headSet(four);
    assertTrue(sm.contains(one));
    assertTrue(sm.contains(two));
    assertTrue(sm.contains(three));
    assertFalse(sm.contains(four));
    assertFalse(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(one, k);
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, set.size());
    assertEquals(four, set.first());
}
 
Example 4
Source Project: istio-fleetman   File: DataBasicInMemoryImpl.java    License: MIT License 5 votes vote down vote up
@Override
public TreeSet<VehiclePosition> getAllReportsForVehicleSince(String name, Date timestamp) throws VehicleNotFoundException {
	if (timestamp == null) timestamp = new java.util.Date(1);
	
	// Could use a Java 8 lambda to filter the collection but I'm playing safe in targeting Java 7
	TreeSet<VehiclePosition> vehicleReports = this.positionDatabase.get(name);
	if (vehicleReports == null) throw new VehicleNotFoundException();
	
	VehiclePosition example = new VehicleBuilder().withName(name).withTimestamp(timestamp).build();
	TreeSet<VehiclePosition> results = (TreeSet<VehiclePosition>)(vehicleReports.headSet(example, true));
	return results;
}
 
Example 5
Source Project: k8s-fleetman   File: DataBasicInMemoryImpl.java    License: MIT License 5 votes vote down vote up
@Override
public TreeSet<VehiclePosition> getAllReportsForVehicleSince(String name, Date timestamp) throws VehicleNotFoundException {
	if (timestamp == null) timestamp = new java.util.Date(1);
	
	// Could use a Java 8 lambda to filter the collection but I'm playing safe in targeting Java 7
	TreeSet<VehiclePosition> vehicleReports = this.positionDatabase.get(name);
	if (vehicleReports == null) throw new VehicleNotFoundException();
	
	VehiclePosition example = new VehicleBuilder().withName(name).withTimestamp(timestamp).build();
	TreeSet<VehiclePosition> results = (TreeSet<VehiclePosition>)(vehicleReports.headSet(example, true));
	return results;
}
 
Example 6
Source Project: sagetv   File: Scheduler.java    License: 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 7
Source Project: sagetv   File: Scheduler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get the element that preceeds the given interval
 * @param set
 * @param interval
 */
static <T extends LongInterval> T getPrevious(TreeSet<T> set, T interval) {
  SortedSet<T> subSet = set.headSet(interval);
  if (!subSet.isEmpty()) {
    return subSet.last();
  }
  return null;
}
 
Example 8
private InetAddress removeClosest(int target, TreeSet<Host> freeHosts) {
    // search for element with target+1 nodes as the result is strictly less
    SortedSet<Host> headSet = freeHosts.headSet(new Host(null, target + 1));
    Host host = null;
    if (headSet.size() == 0) {
        // take the largest element from the tree
        host = freeHosts.last();
    } else {
        host = headSet.last();
    }
    freeHosts.remove(host);
    return host.address;
}
 
Example 9
Source Project: redisson   File: RedissonSortedSetTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testOrder2() {
    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.headSet(6);
}
 
Example 10
Source Project: jstorm   File: AckTransactionBolt.java    License: Apache License 2.0 5 votes vote down vote up
private void removeObsoleteBatches(long commitBatchId) {
    TreeSet<Long> totalBatches = new TreeSet<Long>(batchXorTracker.getBatchIds());
    Set<Long> obsoleteBatches = totalBatches.headSet(commitBatchId);
    if (obsoleteBatches != null && obsoleteBatches.size() > 0) {
        LOG.info("Remove obsolete batches: {}", obsoleteBatches);
        for (Long batchId : obsoleteBatches) {
            batchXorTracker.removeBatch(batchId);
        }
    }
}
 
Example 11
Source Project: jstorm   File: AckTransactionSpout.java    License: Apache License 2.0 5 votes vote down vote up
private void removeObsoleteBatches(long commitBatchId) {
    TreeSet<Long> totalBatches = new TreeSet<Long>(tracker.getBatchIds());
    Set<Long> obsoleteBatches = totalBatches.headSet(commitBatchId);
    if (obsoleteBatches != null && obsoleteBatches.size() > 0) {
        LOG.info("Remove obsolete batches: {}", obsoleteBatches);
        for (Long batchId : obsoleteBatches) {
            tracker.removeBatch(batchId);
        }
    }
}
 
Example 12
Source Project: tlaplus   File: ModuleCoverageInformation.java    License: MIT License 4 votes vote down vote up
static int getHue(final long count, final TreeSet<Long> counts) {
	final int size = counts.size();
	final float r = 240f / size;
	final SortedSet<Long> headSet = counts.headSet(count);
	return BLUE - Math.round(r * (headSet.size() + 1));
}
 
Example 13
Source Project: redisson   File: RedissonSortedSetTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testHeadSetTreeSet() {
    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.headSet(3);
    hs.add(0);

    assertThat(hs).containsExactly(0, 1, 2);

    set.remove(2);

    assertThat(hs).containsExactly(0, 1);

    set.remove(3);

    assertThat(hs).containsExactly(0, 1);

    hs.add(7);
}
 
Example 14
@Test(expected = IllegalArgumentException.class)
public void testHeadSetTreeSet() {
    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.headSet(3);
    hs.add(0);

    assertThat(hs).containsExactly(0, 1, 2);

    set.remove(2);

    assertThat(hs).containsExactly(0, 1);

    set.remove(3);

    assertThat(hs).containsExactly(0, 1);

    hs.add(7);
}