com.google.common.collect.MinMaxPriorityQueue.pollFirst()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(11.5k)|赞(0)|评价(0)|浏览(97)

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

MinMaxPriorityQueue.pollFirst介绍

[英]Removes and returns the least element of this queue, or returns null if the queue is empty.
[中]移除并返回此队列的最小元素,如果队列为空,则返回null。

代码示例

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

/** Regression test for bug found. */
public void testCorrectOrdering_regression() {
 MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create(ImmutableList.of(3, 5, 1, 4, 7));
 List<Integer> expected = ImmutableList.of(1, 3, 4, 5, 7);
 List<Integer> actual = new ArrayList<>(5);
 for (int i = 0; i < expected.size(); i++) {
  actual.add(q.pollFirst());
 }
 assertEquals(expected, actual);
}

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

List<Map.Entry<String, Integer>> polledServers = new ArrayList<>(1);
while (!completedServersQueue.isEmpty()) {
 Map.Entry<String, Integer> server = completedServersQueue.pollFirst();
 if (instanceStateMap.keySet().contains(server.getKey())) {
  polledServers.add(server);

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

public void testCorrectOrdering_mediumHeapsPollFirst() {
 for (int attempts = 0; attempts < reduceIterationsIfGwt(5000); attempts++) {
  int size = new Random().nextInt(256) + 16;
  ArrayList<Integer> elements = createOrderedList(size);
  List<Integer> expected = ImmutableList.copyOf(elements);
  MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create();
  long seed = insertRandomly(elements, q);
  while (!q.isEmpty()) {
   elements.add(q.pollFirst());
  }
  assertEqualsUsingSeed(seed, expected, elements);
 }
}

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

public void testCorrectOrdering_smallHeapsPollFirst() {
 for (int size = 2; size < 16; size++) {
  for (int attempts = 0; attempts < size * (size - 1); attempts++) {
   ArrayList<Integer> elements = createOrderedList(size);
   List<Integer> expected = ImmutableList.copyOf(elements);
   MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create();
   long seed = insertRandomly(elements, q);
   while (!q.isEmpty()) {
    elements.add(q.pollFirst());
   }
   assertEqualsUsingSeed(seed, expected, elements);
  }
 }
}

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

/** Regression test for bug found in random testing. */
public void testCorrectOrdering_73ElementBug() {
 int size = 73;
 long seed = 7522346378524621981L;
 ArrayList<Integer> elements = createOrderedList(size);
 List<Integer> expected = ImmutableList.copyOf(elements);
 MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create();
 insertRandomly(elements, q, new Random(seed));
 assertIntact(q);
 while (!q.isEmpty()) {
  elements.add(q.pollFirst());
  assertIntact(q);
 }
 assertEqualsUsingSeed(seed, expected, elements);
}

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

/** Regression test for b/4124577 */
public void testRegression_dataCorruption() {
 int size = 8;
 List<Integer> expected = createOrderedList(size);
 MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create(expected);
 List<Integer> contents = Lists.newArrayList(expected);
 List<Integer> elements = Lists.newArrayListWithCapacity(size);
 while (!q.isEmpty()) {
  assertThat(q).containsExactlyElementsIn(contents);
  Integer next = q.pollFirst();
  contents.remove(next);
  assertThat(q).containsExactlyElementsIn(contents);
  for (int i = 0; i <= size; i++) {
   q.add(i);
   contents.add(i);
   assertThat(q).containsExactlyElementsIn(contents);
   q.add(next);
   contents.add(next);
   assertThat(q).containsExactlyElementsIn(contents);
   q.remove(i);
   assertTrue(contents.remove(Integer.valueOf(i)));
   assertThat(q).containsExactlyElementsIn(contents);
   assertEquals(next, q.poll());
   contents.remove(next);
   assertThat(q).containsExactlyElementsIn(contents);
  }
  elements.add(next);
 }
 assertEquals(expected, elements);
}

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

public void testExhaustive_pollAndPush() {
 int size = 5;
 List<Integer> expected = createOrderedList(size);
 for (Collection<Integer> perm : Collections2.permutations(expected)) {
  MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create(perm);
  List<Integer> elements = Lists.newArrayListWithCapacity(size);
  while (!q.isEmpty()) {
   Integer next = q.pollFirst();
   for (int i = 0; i <= size; i++) {
    assertTrue(q.add(i));
    assertTrue(q.add(next));
    assertTrue(q.remove(i));
    assertEquals(next, q.poll());
   }
   elements.add(next);
  }
  assertEqualsUsingStartedWith(perm, expected, elements);
 }
}

代码示例来源:origin: forcedotcom/phoenix

IndexedResultEntry re = mergedQueue.pollFirst();
if (re != null) {
  IndexedResultEntry next = null;

代码示例来源:origin: forcedotcom/phoenix

for (int i = 0; i < resSize; i++) {                
  int totalLen = 0;
  ResultEntry re = results.pollFirst();
  List<KeyValue> keyValues = toKeyValues(re);
  for (KeyValue kv : keyValues) {

代码示例来源:origin: co.cask.cdap/cdap-app-fabric

private List<ArtifactDetail> getSortedArtifacts(Row row, int limit,
                        ArtifactSortOrder order, @Nullable ArtifactRange range,
                        Supplier<List<ArtifactDetail>> resultSupplier) {
 // Create a priority queue for remembering the highest/lowest N
 return collectArtifacts(row, range, Integer.MAX_VALUE, Collector.of(
  () -> MinMaxPriorityQueue.orderedBy(Comparator.comparing(ArtifactDetail::getDescriptor)).create(),
  (queue, artifactDetail) -> {
   queue.add(artifactDetail);
   if (queue.size() > limit) {
    // For ascending order, remove the largest. For descending order, remove the smallest.
    if (order == ArtifactSortOrder.ASC) {
     queue.pollLast();
    } else {
     queue.pollFirst();
    }
   }
  }, createUnsupportedCombiner(),
  queue -> {
   List<ArtifactDetail> result = resultSupplier.get();
   while (!queue.isEmpty()) {
    if (order == ArtifactSortOrder.ASC) {
     result.add(queue.pollFirst());
    } else {
     result.add(queue.pollLast());
    }
   }
   return result;
  }));
}

代码示例来源:origin: com.google.guava/guava-tests

/**
 * Regression test for bug found.
 */
public void testCorrectOrdering_regression() {
 MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue
   .create(ImmutableList.of(3, 5, 1, 4, 7));
 List<Integer> expected = ImmutableList.of(1, 3, 4, 5, 7);
 List<Integer> actual = new ArrayList<>(5);
 for (int i = 0; i < expected.size(); i++) {
  actual.add(q.pollFirst());
 }
 assertEquals(expected, actual);
}

代码示例来源:origin: cdapio/cdap

private List<ArtifactDetail> getSortedArtifacts(Iterator<StructuredRow> iterator, int limit,
                        ArtifactSortOrder order, @Nullable ArtifactRange range,
                        Supplier<List<ArtifactDetail>> resultSupplier) {
 // Create a priority queue for remembering the highest/lowest N
 return collectArtifacts(iterator, range, Integer.MAX_VALUE, Collector.of(
  () -> MinMaxPriorityQueue.orderedBy(Comparator.comparing(ArtifactDetail::getDescriptor)).create(),
  (queue, artifactDetail) -> {
   queue.add(artifactDetail);
   if (queue.size() > limit) {
    // For ascending order, remove the largest. For descending order, remove the smallest.
    if (order == ArtifactSortOrder.ASC) {
     queue.pollLast();
    } else {
     queue.pollFirst();
    }
   }
  }, createUnsupportedCombiner(),
  queue -> {
   List<ArtifactDetail> result = resultSupplier.get();
   while (!queue.isEmpty()) {
    if (order == ArtifactSortOrder.ASC) {
     result.add(queue.pollFirst());
    } else {
     result.add(queue.pollLast());
    }
   }
   return result;
  }));
}

代码示例来源:origin: com.google.guava/guava-tests

public void testCorrectOrdering_mediumHeapsPollFirst() {
 for (int attempts = 0; attempts < 5000; attempts++) {
  int size = new Random().nextInt(256) + 16;
  ArrayList<Integer> elements = createOrderedList(size);
  List<Integer> expected = ImmutableList.copyOf(elements);
  MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create();
  long seed = insertRandomly(elements, q);
  while (!q.isEmpty()) {
   elements.add(q.pollFirst());
  }
  assertEquals("Using seed " + seed, expected, elements);
 }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testCorrectOrdering_randomAccess() {
 long seed = new Random().nextLong();
 Random random = new Random(seed);
 PriorityQueue<Integer> control = new PriorityQueue<>();
 MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create();
 for (int i = 0; i < 73; i++) { // 73 is a childless uncle case.
  Integer element = random.nextInt();
  control.add(element);
  assertTrue(q.add(element));
 }
 assertTrue("State " + Arrays.toString(q.toArray()), q.isIntact());
 for (int i = 0; i < 500000; i++) {
  if (random.nextBoolean()) {
   Integer element = random.nextInt();
   control.add(element);
   q.add(element);
  } else {
   assertEquals("Using seed " + seed, control.poll(), q.pollFirst());
  }
 }
 while (!control.isEmpty()) {
  assertEquals("Using seed " + seed, control.poll(), q.pollFirst());
 }
 assertTrue(q.isEmpty());
}

代码示例来源:origin: com.google.guava/guava-tests

public void testCorrectOrdering_smallHeapsPollFirst() {
 for (int size = 2; size < 16; size++) {
  for (int attempts = 0; attempts < size * (size - 1); attempts++) {
   ArrayList<Integer> elements = createOrderedList(size);
   List<Integer> expected = ImmutableList.copyOf(elements);
   MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create();
   long seed = insertRandomly(elements, q);
   while (!q.isEmpty()) {
    elements.add(q.pollFirst());
   }
   assertEquals("Using seed " + seed, expected, elements);
  }
 }
}

代码示例来源:origin: linkfluence/fastText4j

private void predict(List<Integer> words, List<FastTextPrediction> predictions, int k, float threshold) {
 if (!words.isEmpty()) {
  Vector hidden = new Vector(args.getDimension());
  Vector output = new Vector(dict.nLabels());
  MinMaxPriorityQueue<Pair<Float, Integer>> modelPredictions = MinMaxPriorityQueue
   .orderedBy(new Model.HeapComparator<Integer>())
   .expectedSize(dict.nLabels())
   .create();
  int[] input = Ints.toArray(words);
  if (words.isEmpty()) {
   return;
  }
  model.predict(input, k, threshold, modelPredictions, hidden, output);
  while (!modelPredictions.isEmpty()) {
   Pair<Float, Integer> pred = modelPredictions.pollFirst();
   predictions.add(new FastTextPrediction(dict.getLabel(pred.last()), pred.first()));
  }
 }
}

代码示例来源:origin: linkfluence/fastText4j

private List<FastTextSynonym> findNN(Vector queryVec, int k, Set<String> banSet) {
 precomputeWordVectors();
 float queryNorm = queryVec.norm();
 if (Math.abs(queryNorm) < 1e-8) {
  queryNorm = 1.0f;
 }
 MinMaxPriorityQueue<Pair<Float, String>> heap = MinMaxPriorityQueue
  .orderedBy(new Model.HeapComparator<String>())
  .expectedSize(dict.nLabels())
  .create();
 for (int i = 0; i < dict.nWords(); i++) {
  String word = dict.getWord(i);
  float dp = wordVectors.dotRow(queryVec, i);
  heap.add(new Pair<>(dp / queryNorm, word));
 }
 List<FastTextSynonym> syns = new ArrayList<>();
 int i = 0;
 while (i < k && heap.size() > 0) {
  Pair<Float, String> synonym = heap.pollFirst();
  boolean banned = banSet.contains(synonym.last());
  if (!banned) {
   syns.add(new FastTextSynonym(synonym.last(), synonym.first()));
   i++;
  }
 }
 return syns;
}

代码示例来源:origin: com.google.guava/guava-tests

while (!q.isEmpty()) {
 assertThat(q).containsExactlyElementsIn(contents);
 Integer next = q.pollFirst();
 contents.remove(next);
 assertThat(q).containsExactlyElementsIn(contents);

代码示例来源:origin: com.google.guava/guava-tests

/**
 * Regression test for bug found in random testing.
 */
public void testCorrectOrdering_73ElementBug() {
 int size = 73;
 long seed = 7522346378524621981L;
 ArrayList<Integer> elements = createOrderedList(size);
 List<Integer> expected = ImmutableList.copyOf(elements);
 MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create();
 insertRandomly(elements, q, new Random(seed));
 assertTrue(q.isIntact());
 while (!q.isEmpty()) {
  elements.add(q.pollFirst());
  assertTrue("State " + Arrays.toString(q.toArray()), q.isIntact());
 }
 assertEquals("Using seed " + seed, expected, elements);
}

代码示例来源:origin: com.google.guava/guava-tests

public void testExhaustive_pollAndPush() {
 int size = 5;
 List<Integer> expected = createOrderedList(size);
 for (Collection<Integer> perm : Collections2.permutations(expected)) {
  MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create(perm);
  List<Integer> elements = Lists.newArrayListWithCapacity(size);
  while (!q.isEmpty()) {
   Integer next = q.pollFirst();
   for (int i = 0; i <= size; i++) {
    assertTrue(q.add(i));
    assertTrue(q.add(next));
    assertTrue(q.remove(i));
    assertEquals(next, q.poll());
   }
   elements.add(next);
  }
  assertEquals("Started with " + perm, expected, elements);
 }
}

相关文章