本文整理汇总了Java中java.util.NavigableMap.headMap方法的典型用法代码示例。如果您正苦于以下问题:Java NavigableMap.headMap方法的具体用法?Java NavigableMap.headMap怎么用?Java NavigableMap.headMap使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.NavigableMap
的用法示例。
在下文中一共展示了NavigableMap.headMap方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: subMap
import java.util.NavigableMap; //导入方法依赖的package包/类
/**
* Returns a view of the portion of {@code map} whose keys are contained by {@code range}.
*
* <p>This method delegates to the appropriate methods of {@link NavigableMap} (namely
* {@link NavigableMap#subMap(Object, boolean, Object, boolean) subMap()},
* {@link NavigableMap#tailMap(Object, boolean) tailMap()}, and
* {@link NavigableMap#headMap(Object, boolean) headMap()}) to actually construct the view.
* Consult these methods for a full description of the returned view's behavior.
*
* <p><b>Warning:</b> {@code Range}s always represent a range of values using the values' natural
* ordering. {@code NavigableMap} on the other hand can specify a custom ordering via a
* {@link Comparator}, which can violate the natural ordering. Using this method (or in general
* using {@code Range}) with unnaturally-ordered maps can lead to unexpected and undefined
* behavior.
*
* @since 20.0
*/
@Beta
@GwtIncompatible // NavigableMap
public static <K extends Comparable<? super K>, V> NavigableMap<K, V> subMap(
NavigableMap<K, V> map, Range<K> range) {
if (map.comparator() != null
&& map.comparator() != Ordering.natural()
&& range.hasLowerBound()
&& range.hasUpperBound()) {
checkArgument(
map.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0,
"map is using a custom comparator which is inconsistent with the natural ordering.");
}
if (range.hasLowerBound() && range.hasUpperBound()) {
return map.subMap(
range.lowerEndpoint(),
range.lowerBoundType() == BoundType.CLOSED,
range.upperEndpoint(),
range.upperBoundType() == BoundType.CLOSED);
} else if (range.hasLowerBound()) {
return map.tailMap(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED);
} else if (range.hasUpperBound()) {
return map.headMap(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
}
return checkNotNull(map);
}
示例2: createSubMap
import java.util.NavigableMap; //导入方法依赖的package包/类
@Override
NavigableMap<K, V> createSubMap(SortedMap<K, V> sortedMap, K firstExclusive, K lastExclusive) {
NavigableMap<K, V> map = (NavigableMap<K, V>) sortedMap;
if (from == Bound.NO_BOUND && to == Bound.INCLUSIVE) {
return map.headMap(lastInclusive, true);
} else if (from == Bound.EXCLUSIVE && to == Bound.NO_BOUND) {
return map.tailMap(firstExclusive, false);
} else if (from == Bound.EXCLUSIVE && to == Bound.EXCLUSIVE) {
return map.subMap(firstExclusive, false, lastExclusive, false);
} else if (from == Bound.EXCLUSIVE && to == Bound.INCLUSIVE) {
return map.subMap(firstExclusive, false, lastInclusive, true);
} else if (from == Bound.INCLUSIVE && to == Bound.INCLUSIVE) {
return map.subMap(firstInclusive, true, lastInclusive, true);
} else {
return (NavigableMap<K, V>) super.createSubMap(map, firstExclusive, lastExclusive);
}
}
示例3: testheadMapRanges
import java.util.NavigableMap; //导入方法依赖的package包/类
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testheadMapRanges(String description, NavigableMap navigableMap) {
NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);
// same subset
subMap.headMap(BigInteger.ONE, true);
// slightly smaller
NavigableMap ns = subMap.headMap(BigInteger.ONE, false);
// slight exapansion
assertThrows(() -> {
ns.headMap(BigInteger.ONE, true);
},
IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
}
示例4: testheadMapRanges
import java.util.NavigableMap; //导入方法依赖的package包/类
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testheadMapRanges(String description, NavigableMap navigableMap) {
NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);
// same subset
subMap.headMap(BigInteger.ONE, true);
// slightly smaller
NavigableMap ns = subMap.headMap(BigInteger.ONE, false);
// slight expansion
assertThrowsIAE(() -> {
ns.headMap(BigInteger.ONE, true);
},
description + ": Expansion should not be allowed");
// much smaller
subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
}
示例5: testHeadMapContents
import java.util.NavigableMap; //导入方法依赖的package包/类
/**
* headMap returns map with keys in requested range
*/
public void testHeadMapContents() {
NavigableMap map = map5();
SortedMap sm = map.headMap(four);
assertTrue(sm.containsKey(one));
assertTrue(sm.containsKey(two));
assertTrue(sm.containsKey(three));
assertFalse(sm.containsKey(four));
assertFalse(sm.containsKey(five));
Iterator i = sm.keySet().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, map.size());
assertEquals(four, map.firstKey());
}
示例6: testDescendingHeadMapContents
import java.util.NavigableMap; //导入方法依赖的package包/类
/**
* headMap returns map with keys in requested range
*/
public void testDescendingHeadMapContents() {
NavigableMap map = dmap5();
SortedMap sm = map.headMap(m4);
assertTrue(sm.containsKey(m1));
assertTrue(sm.containsKey(m2));
assertTrue(sm.containsKey(m3));
assertFalse(sm.containsKey(m4));
assertFalse(sm.containsKey(m5));
Iterator i = sm.keySet().iterator();
Object k;
k = (Integer)(i.next());
assertEquals(m1, k);
k = (Integer)(i.next());
assertEquals(m2, k);
k = (Integer)(i.next());
assertEquals(m3, k);
assertFalse(i.hasNext());
sm.clear();
assertTrue(sm.isEmpty());
assertEquals(2, map.size());
assertEquals(m4, map.firstKey());
}
示例7: testHeadMap_K_B
import java.util.NavigableMap; //导入方法依赖的package包/类
public void testHeadMap_K_B() {
NavigableMap<String, Integer> map = create();
NavigableMap<String, Integer> headMap = map.headMap("a", true);
assertTrue(headMap instanceof SynchronizedNavigableMap);
assertSame(
mutex, ((SynchronizedNavigableMap<String, Integer>) headMap).mutex);
}
示例8: testAsMapNavigableReadsThrough
import java.util.NavigableMap; //导入方法依赖的package包/类
@GwtIncompatible // NavigableMap
public void testAsMapNavigableReadsThrough() {
NavigableSet<String> strings = Sets.newTreeSet();
Collections.addAll(strings, "one", "two", "three");
NavigableMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION);
assertNull(map.comparator());
assertEquals(ImmutableSortedMap.of("one", 3, "two", 3, "three", 5), map);
assertNull(map.get("four"));
strings.add("four");
assertEquals(
ImmutableSortedMap.of("one", 3, "two", 3, "three", 5, "four", 4),
map);
assertEquals(Integer.valueOf(4), map.get("four"));
SortedMap<String, Integer> headMap = map.headMap("two");
assertEquals(
ImmutableSortedMap.of("four", 4, "one", 3, "three", 5),
headMap);
strings.add("five");
strings.remove("one");
assertEquals(
ImmutableSortedMap.of("five", 4, "four", 4, "three", 5),
headMap);
assertThat(map.entrySet()).containsExactly(
mapEntry("five", 4),
mapEntry("four", 4),
mapEntry("three", 5),
mapEntry("two", 3)).inOrder();
NavigableMap<String, Integer> tailMap = map.tailMap("s", true);
NavigableMap<String, Integer> subMap = map.subMap("a", true, "t", false);
strings.add("six");
strings.remove("two");
assertThat(tailMap.entrySet()).containsExactly(
mapEntry("six", 3),
mapEntry("three", 5)).inOrder();
assertThat(subMap.entrySet()).containsExactly(
mapEntry("five", 4),
mapEntry("four", 4),
mapEntry("six", 3)).inOrder();
}
示例9: testHeadMap_K
import java.util.NavigableMap; //导入方法依赖的package包/类
public void testHeadMap_K() {
NavigableMap<String, Integer> map = create();
SortedMap<String, Integer> headMap = map.headMap("a");
assertTrue(headMap instanceof SynchronizedSortedMap);
assertSame(mutex, ((SynchronizedSortedMap<String, Integer>) headMap).mutex);
}