java.util.SortedSet类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(171)

本文整理了Java中java.util.SortedSet类的一些代码示例,展示了SortedSet类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SortedSet类的具体详情如下:
包路径:java.util.SortedSet
类名称:SortedSet

SortedSet介绍

[英]SortedSet is a Set which iterates over its elements in a sorted order. The order is determined either by the elements natural ordering, or by a Comparator which is passed into a concrete implementation at construction time. All elements in this set must be mutually comparable. The ordering in this set must be consistent with equals of its elements.
[中]SortedSet是按排序顺序在其元素上迭代的集合。顺序由元素的自然顺序决定,或者由在构建时传递到具体实现中的比较器决定。此集合中的所有元素必须相互可比。这个集合中的顺序必须与其元素的等号一致。

代码示例

代码示例来源:origin: google/guava

private static <E extends Comparable<? super E>> SortedSet<E> nullCheckedTreeSet(E[] elements) {
 SortedSet<E> set = newTreeSet();
 for (E element : elements) {
  // Explicit null check because TreeSet wrongly accepts add(null) when empty.
  set.add(checkNotNull(element));
 }
 return set;
}

代码示例来源:origin: spring-projects/spring-framework

/**
   * Find the requested number of available ports for this {@code SocketType},
   * each randomly selected from the range [{@code minPort}, {@code maxPort}].
   * @param numRequested the number of available ports to find
   * @param minPort the minimum port number
   * @param maxPort the maximum port number
   * @return a sorted set of available port numbers for this socket type
   * @throws IllegalStateException if the requested number of available ports could not be found
   */
  SortedSet<Integer> findAvailablePorts(int numRequested, int minPort, int maxPort) {
    Assert.isTrue(minPort > 0, "'minPort' must be greater than 0");
    Assert.isTrue(maxPort > minPort, "'maxPort' must be greater than 'minPort'");
    Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);
    Assert.isTrue(numRequested > 0, "'numRequested' must be greater than 0");
    Assert.isTrue((maxPort - minPort) >= numRequested,
        "'numRequested' must not be greater than 'maxPort' - 'minPort'");
    SortedSet<Integer> availablePorts = new TreeSet<>();
    int attemptCount = 0;
    while ((++attemptCount <= numRequested + 100) && availablePorts.size() < numRequested) {
      availablePorts.add(findAvailablePort(minPort, maxPort));
    }
    if (availablePorts.size() != numRequested) {
      throw new IllegalStateException(String.format(
          "Could not find %d available %s ports in the range [%d, %d]",
          numRequested, name(), minPort, maxPort));
    }
    return availablePorts;
  }
}

代码示例来源:origin: google/guava

@Override
 protected SortedSet<Integer> create(Integer[] elements) {
  SortedSet<Integer> set = nullCheckedTreeSet(elements);
  if (set.isEmpty()) {
   /*
    * The (tooLow + 1, tooHigh) arguments below would be invalid because tooLow would be
    * greater than tooHigh.
    */
   return ContiguousSet.create(Range.openClosed(0, 1), DiscreteDomain.integers()).subSet(0, 1);
  }
  int tooHigh = set.last() + 1;
  int tooLow = set.first() - 1;
  set.add(tooHigh);
  set.add(tooLow);
  return checkedCreate(set).subSet(tooLow + 1, tooHigh);
 }
}

代码示例来源:origin: google/guava

@Override
 public E last() {
  SortedSet<E> sortedUnfiltered = (SortedSet<E>) unfiltered;
  while (true) {
   E element = sortedUnfiltered.last();
   if (predicate.apply(element)) {
    return element;
   }
   sortedUnfiltered = sortedUnfiltered.headSet(element);
  }
 }
}

代码示例来源:origin: google/guava

@Override
 protected SortedSet<Integer> create(Integer[] elements) {
  SortedSet<Integer> set = nullCheckedTreeSet(elements);
  int tooHigh = set.isEmpty() ? 0 : set.last() + 1;
  set.add(tooHigh);
  return checkedCreate(set).headSet(tooHigh);
 }
}

代码示例来源:origin: google/guava

@Override
 protected SortedSet<Integer> create(Integer[] elements) {
  SortedSet<Integer> set = nullCheckedTreeSet(elements);
  int tooLow = set.isEmpty() ? 0 : set.first() - 1;
  set.add(tooLow);
  return checkedCreate(set).tailSet(tooLow + 1);
 }
}

代码示例来源:origin: google/guava

void ensureNotDirectlyModifiable(SortedSet<Integer> unmod) {
 try {
  unmod.add(4);
  fail("UnsupportedOperationException expected");
 } catch (UnsupportedOperationException expected) {
 }
 try {
  unmod.remove(4);
  fail("UnsupportedOperationException expected");
 } catch (UnsupportedOperationException expected) {
 }
 try {
  unmod.addAll(Collections.singleton(4));
  fail("UnsupportedOperationException expected");
 } catch (UnsupportedOperationException expected) {
 }
 try {
  Iterator<Integer> iterator = unmod.iterator();
  iterator.next();
  iterator.remove();
  fail("UnsupportedOperationException expected");
 } catch (UnsupportedOperationException expected) {
 }
}

代码示例来源:origin: google/guava

public void testSortedKeySet() {
 TreeMultimap<String, Integer> multimap = createPopulate();
 SortedSet<String> keySet = multimap.keySet();
 assertEquals(null, keySet.first());
 assertEquals("google", keySet.last());
 assertEquals(StringLength.COMPARATOR, keySet.comparator());
 assertEquals(Sets.newHashSet(null, "tree"), keySet.headSet("yahoo"));
 assertEquals(Sets.newHashSet("google"), keySet.tailSet("yahoo"));
 assertEquals(Sets.newHashSet("tree"), keySet.subSet("ask", "yahoo"));
}

代码示例来源:origin: google/guava

@GwtIncompatible // SerializableTester
 public void testSeveral_serialization() {
  SortedSet<String> set = new SafeTreeSet<>();
  set.add("a");
  set.add("b");
  set.add("c");
  SortedSet<String> copy = SerializableTester.reserializeAndAssert(set);
  assertEquals(set.comparator(), copy.comparator());
 }
}

代码示例来源:origin: apache/incubator-druid

@Override
public DimFilter optimize()
{
 InDimFilter inFilter = optimizeLookup();
 if (inFilter.values.size() == 1) {
  return new SelectorDimFilter(inFilter.dimension, inFilter.values.first(), inFilter.getExtractionFn());
 }
 return inFilter;
}

代码示例来源:origin: apache/hbase

/**
 * Adds the given servers to the group.
 */
public void addAllServers(Collection<Address> hostPort){
 servers.addAll(hostPort);
}

代码示例来源:origin: apache/storm

@Override
public KinesisMessageId getNextFailedMessageToRetry() {
  KinesisMessageId result = null;
  // return the first message to be retried from the set. It will return the message with the earliest retry time <= current time
  if (!retryMessageSet.isEmpty() ) {
    result = retryMessageSet.first();
    if (!(retryTimes.get(result) <= System.nanoTime())) {
      result = null;
    }
  }
  LOG.debug("Returning {} to spout for retrying.", result);
  return result;
}

代码示例来源:origin: jenkinsci/jenkins

public synchronized int size() {
  return permissions.size();
}

代码示例来源:origin: google/guava

public void testAsMapSortedReadsThrough() {
 SortedSet<String> strings = new NonNavigableSortedSet();
 Collections.addAll(strings, "one", "two", "three");
 SortedMap<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();
}

代码示例来源:origin: google/j2objc

@Override
public K firstKey() {
 // correctly throws NoSuchElementException when filtered map is empty.
 return keySet().iterator().next();
}

代码示例来源:origin: google/guava

@Override
public E first() {
 synchronized (mutex) {
  return delegate().first();
 }
}

代码示例来源:origin: spring-projects/spring-framework

@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests
@Test
public void setCollectionPropertyWithIntegerValue() {
  IndexedTestBean target = new IndexedTestBean();
  AbstractPropertyAccessor accessor = createAccessor(target);
  Collection<Integer> coll = new HashSet<>();
  coll.add(0);
  accessor.setPropertyValue("collection", new Integer(0));
  List<Integer> set = new LinkedList<>();
  set.add(1);
  accessor.setPropertyValue("set", new Integer(1));
  List<Integer> sortedSet = new ArrayList<>();
  sortedSet.add(2);
  accessor.setPropertyValue("sortedSet", new Integer(2));
  Set<Integer> list = new HashSet<>();
  list.add(3);
  accessor.setPropertyValue("list", new Integer(3));
  assertEquals(1, target.getCollection().size());
  assertTrue(target.getCollection().containsAll(coll));
  assertEquals(1, target.getSet().size());
  assertTrue(target.getSet().containsAll(set));
  assertEquals(1, target.getSortedSet().size());
  assertTrue(target.getSortedSet().containsAll(sortedSet));
  assertEquals(1, target.getList().size());
  assertTrue(target.getList().containsAll(list));
}

代码示例来源:origin: google/guava

@Override
public E last() {
 synchronized (mutex) {
  return delegate().last();
 }
}

代码示例来源:origin: google/guava

public void testNullAcceptingComparator() throws Exception {
 Comparator<String> comparator = Ordering.<String>natural().nullsFirst();
 TreeMultiset<String> ms = TreeMultiset.create(comparator);
 ms.add("b");
 ms.add(null);
 ms.add("a");
 ms.add("b");
 ms.add(null, 2);
 assertThat(ms).containsExactly(null, null, null, "a", "b", "b").inOrder();
 assertEquals(3, ms.count(null));
 SortedSet<String> elementSet = ms.elementSet();
 assertEquals(null, elementSet.first());
 assertEquals("b", elementSet.last());
 assertEquals(comparator, elementSet.comparator());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void setCollectionPropertyWithStringValueAndCustomEditor() {
  IndexedTestBean target = new IndexedTestBean();
  AbstractPropertyAccessor accessor = createAccessor(target);
  accessor.registerCustomEditor(String.class, "set", new StringTrimmerEditor(false));
  accessor.registerCustomEditor(String.class, "list", new StringTrimmerEditor(false));
  accessor.setPropertyValue("set", "set1 ");
  accessor.setPropertyValue("sortedSet", "sortedSet1");
  accessor.setPropertyValue("list", "list1 ");
  assertEquals(1, target.getSet().size());
  assertTrue(target.getSet().contains("set1"));
  assertEquals(1, target.getSortedSet().size());
  assertTrue(target.getSortedSet().contains("sortedSet1"));
  assertEquals(1, target.getList().size());
  assertTrue(target.getList().contains("list1"));
  accessor.setPropertyValue("list", Collections.singletonList("list1 "));
  assertTrue(target.getList().contains("list1"));
}

相关文章