当前位置: 首页>>代码示例>>Java>>正文


Java NavigableSet.comparator方法代码示例

本文整理汇总了Java中java.util.NavigableSet.comparator方法的典型用法代码示例。如果您正苦于以下问题:Java NavigableSet.comparator方法的具体用法?Java NavigableSet.comparator怎么用?Java NavigableSet.comparator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.NavigableSet的用法示例。


在下文中一共展示了NavigableSet.comparator方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: subSet

import java.util.NavigableSet; //导入方法依赖的package包/类
/**
 * Returns a view of the portion of {@code set} whose elements are contained by {@code range}.
 *
 * <p>This method delegates to the appropriate methods of {@link NavigableSet} (namely
 * {@link NavigableSet#subSet(Object, boolean, Object, boolean) subSet()},
 * {@link NavigableSet#tailSet(Object, boolean) tailSet()}, and
 * {@link NavigableSet#headSet(Object, boolean) headSet()}) 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 NavigableSet} 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 sets can lead to unexpected and undefined
 * behavior.
 *
 * @since 20.0
 */
@Beta
@GwtIncompatible // NavigableSet
public static <K extends Comparable<? super K>> NavigableSet<K> subSet(
    NavigableSet<K> set, Range<K> range) {
  if (set.comparator() != null
      && set.comparator() != Ordering.natural()
      && range.hasLowerBound()
      && range.hasUpperBound()) {
    checkArgument(
        set.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0,
        "set is using a custom comparator which is inconsistent with the natural ordering.");
  }
  if (range.hasLowerBound() && range.hasUpperBound()) {
    return set.subSet(
        range.lowerEndpoint(),
        range.lowerBoundType() == BoundType.CLOSED,
        range.upperEndpoint(),
        range.upperBoundType() == BoundType.CLOSED);
  } else if (range.hasLowerBound()) {
    return set.tailSet(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED);
  } else if (range.hasUpperBound()) {
    return set.headSet(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
  }
  return checkNotNull(set);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:43,代码来源:Sets.java

示例2: testComparatorIsNull

import java.util.NavigableSet; //导入方法依赖的package包/类
/**
 * Tests that the comparator is {@code null}.
 */
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testComparatorIsNull(String description, NavigableSet<?> navigableSet) {
    Comparator comparator = navigableSet.comparator();

    assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:10,代码来源:EmptyNavigableSet.java


注:本文中的java.util.NavigableSet.comparator方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。