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

The following examples show how to use java.util.TreeSet#headSet() . 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: TreeSetTest.java    From openjdk-jdk9 with GNU General Public License v2.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 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 j2objc with 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 File: DataBasicInMemoryImpl.java    From istio-fleetman with 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 File: DataBasicInMemoryImpl.java    From k8s-fleetman with 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 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 7
Source File: Scheduler.java    From sagetv with 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
Source File: TopologyManager.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
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 File: RedissonSortedSetTest.java    From redisson with 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 File: AckTransactionBolt.java    From jstorm with 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 File: AckTransactionSpout.java    From jstorm with 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 File: ModuleCoverageInformation.java    From tlaplus with 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 File: RedissonSortedSetTest.java    From redisson with 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
Source File: RedissonScoredSortedSetTest.java    From redisson with 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);
}