Java Code Examples for java.util.TreeMap#higherEntry()

The following examples show how to use java.util.TreeMap#higherEntry() . 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
private void test32Collision(String message, Method method, Object[] datas, Collection<Object> hashes, TreeMap<Integer, Integer> counts) throws Exception {
    double size = 1000D;
    double step = Integer.MAX_VALUE;
    step -= Integer.MIN_VALUE;
    step /= size;

    for (int index = 0; index < size; index++) {
        counts.put((int) (Integer.MAX_VALUE - step * index), 0);
    }

    long time = System.currentTimeMillis();
    int collision = 0;
    for (Object data : datas) {
        Integer hash = Integer.class.cast(method.invoke(null, data));
        Entry<Integer, Integer> term = counts.higherEntry(hash);
        counts.put(term.getKey(), term.getValue() + 1);
        if (!hashes.add(hash)) {
            collision++;
        }
    }

    message = StringUtility.format("{}使用[{}]算法,哈希{}次,冲突{}次,方差{},时间{}毫秒", message, method.getName(), datas.length, collision, (long) getVariance(counts.values()), System.currentTimeMillis() - time);
    logger.debug(message);
    hashes.clear();
    counts.clear();
}
 
Example 2
private void test64Collision(String message, Method method, Object[] datas, Collection<Object> hashes, TreeMap<Long, Integer> counts) throws Exception {
    double size = 1000D;
    double step = Long.MAX_VALUE;
    step -= Long.MIN_VALUE;
    step /= size;

    for (int index = 0; index < size; index++) {
        counts.put((long) (Long.MAX_VALUE - step * index), 0);
    }

    long time = System.currentTimeMillis();
    int collision = 0;
    for (Object data : datas) {
        Long hash = Long.class.cast(method.invoke(null, data.toString()));
        Entry<Long, Integer> term = counts.higherEntry(hash);
        counts.put(term.getKey(), term.getValue() + 1);
        if (!hashes.add(hash)) {
            collision++;
        }
    }

    message = StringUtility.format("{}使用[{}]算法,哈希{}次,冲突{}次,方差{},时间{}毫秒", message, method.getName(), datas.length, collision, (long) getVariance(counts.values()), System.currentTimeMillis() - time);
    logger.debug(message);
    hashes.clear();
    counts.clear();
}
 
Example 3
/**
 * higherEntry returns next entry.
 */
public void testHigherEntry() {
    TreeMap map = map5();
    Map.Entry e1 = map.higherEntry(three);
    assertEquals(four, e1.getKey());

    Map.Entry e2 = map.higherEntry(zero);
    assertEquals(one, e2.getKey());

    Map.Entry e3 = map.higherEntry(five);
    assertNull(e3);

    Map.Entry e4 = map.higherEntry(six);
    assertNull(e4);
}
 
Example 4
Source Project: j2objc   File: TreeMapTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * higherEntry returns next entry.
 */
public void testHigherEntry() {
    TreeMap map = map5();
    Map.Entry e1 = map.higherEntry(three);
    assertEquals(four, e1.getKey());

    Map.Entry e2 = map.higherEntry(zero);
    assertEquals(one, e2.getKey());

    Map.Entry e3 = map.higherEntry(five);
    assertNull(e3);

    Map.Entry e4 = map.higherEntry(six);
    assertNull(e4);
}
 
Example 5
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
    if (nums.length == 0 || k == 0) {
        return false;
    }
    TreeMap<Integer, Integer> map = new TreeMap<>();
    for (int i = 0; i < nums.length; i++) {
        int lowerEntry = nums[i] - t - 1;
        int higherEntry = nums[i] + t + 1;
        Map.Entry<Integer, Integer> higher = map.lowerEntry(higherEntry);
        if (higher != null && higher.getKey() >= nums[i]) {
            return true;
        }
        Map.Entry<Integer, Integer> lower = map.higherEntry(lowerEntry);
        if (lower != null && lower.getKey() <= nums[i]) {
            return true;
        }
        if (map.size() == k) {
            map.compute(nums[i - k], (key, val) -> {
                if (val == 1) {
                    return null;
                } else {
                    return val - 1;
                }
            });
        }
        map.compute(nums[i], (key, val) -> {
            if (val == null) {
                return 1;
            } else {
                return val + 1;
            }
        });
    }
    return false;
}
 
Example 6
Source Project: JavaTutorial   File: TreeMapExample.java    License: MIT License 4 votes vote down vote up
public void getHigherEntry(TreeMap<String,String> maps,String key){
    Map.Entry<String,String> entry = maps.higherEntry(key);
    System.out.println("后一个的Entry如下");
    System.out.print("key = " + entry.getKey());
    System.out.println(" value = " + entry.getValue());
}
 
Example 7
Source Project: ambry   File: IndexTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Tests all cases of {@link PersistentIndex#findEntriesSince(FindToken, long)} that result in an index based
 * {@link StoreFindToken} being returned.
 * 1. Uninited -> Index
 * 2. Index -> Index
 * 3. Journal -> Index
 * @throws StoreException
 */
private void findEntriesSinceToIndexBasedTest() throws StoreException {
  // ------------------
  // 1. Index -> Index
  Offset firstIndexSegmentStartOffset = state.referenceIndex.firstKey();
  Offset secondIndexSegmentStartOffset = state.referenceIndex.higherKey(firstIndexSegmentStartOffset);
  MockId firstId = state.referenceIndex.get(firstIndexSegmentStartOffset).firstKey();
  // All elements from first index segment and two from the second to be returned (because of size restrictions)
  Set<MockId> expectedKeys = new HashSet<>();
  long maxTotalSizeOfEntries = 0;
  for (Map.Entry<MockId, TreeSet<IndexValue>> segmentEntry : state.referenceIndex.get(firstIndexSegmentStartOffset)
      .entrySet()) {
    if (!segmentEntry.getKey().equals(firstId)) {
      expectedKeys.add(segmentEntry.getKey());
      maxTotalSizeOfEntries += getSizeOfAllValues(segmentEntry.getValue());
    }
  }
  TreeMap<MockId, TreeSet<IndexValue>> secondIndexSegment = state.referenceIndex.get(secondIndexSegmentStartOffset);
  Map.Entry<MockId, TreeSet<IndexValue>> secondIndexSegmentEntry = secondIndexSegment.firstEntry();
  expectedKeys.add(secondIndexSegmentEntry.getKey());
  maxTotalSizeOfEntries += getSizeOfAllValues(secondIndexSegmentEntry.getValue());
  secondIndexSegmentEntry = secondIndexSegment.higherEntry(secondIndexSegmentEntry.getKey());
  expectedKeys.add(secondIndexSegmentEntry.getKey());
  maxTotalSizeOfEntries += getSizeOfAllValues(secondIndexSegmentEntry.getValue());

  StoreFindToken startToken =
      new StoreFindToken(firstId, firstIndexSegmentStartOffset, state.sessionId, state.incarnationId);
  StoreFindToken expectedEndToken =
      new StoreFindToken(secondIndexSegmentEntry.getKey(), secondIndexSegmentStartOffset, state.sessionId,
          state.incarnationId);
  expectedEndToken.setBytesRead(state.index.getAbsolutePositionInLogForOffset(secondIndexSegmentStartOffset));
  doFindEntriesSinceTest(startToken, maxTotalSizeOfEntries, expectedKeys, expectedEndToken);

  // ------------------
  // 2. Uninitialized -> Index
  // add firstStoreKey and its size
  expectedKeys.add(firstId);
  maxTotalSizeOfEntries +=
      getSizeOfAllValues(state.referenceIndex.get(firstIndexSegmentStartOffset).firstEntry().getValue());
  doFindEntriesSinceTest(new StoreFindToken(), maxTotalSizeOfEntries, expectedKeys, expectedEndToken);

  // ------------------
  // 3. Journal -> Index
  // create a journal based token for an offset that isn't in the journal
  startToken = new StoreFindToken(state.logOrder.firstKey(), state.sessionId, state.incarnationId, false);
  doFindEntriesSinceTest(startToken, maxTotalSizeOfEntries, expectedKeys, expectedEndToken);
}