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

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

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

MinMaxPriorityQueue.iterator介绍

[英]Returns an iterator over the elements contained in this collection, in no particular order.

The iterator is fail-fast: If the MinMaxPriorityQueue is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will generally throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
[中]返回此集合中包含的元素的迭代器,没有特定顺序。
迭代器是快速失败的:如果在创建迭代器后的任何时间以任何方式修改MinMaxPriorityQueue,除了通过迭代器自己的remove方法之外,迭代器通常会抛出ConcurrentModificationException。因此,在面对并发修改时,迭代器会快速、干净地失败,而不是在未来的不确定时间冒着任意、不确定性行为的风险。
请注意,迭代器的快速失效行为无法得到保证,因为一般来说,在存在非同步并发修改的情况下,不可能做出任何硬保证。快速失败的迭代器会尽最大努力抛出ConcurrentModificationException。因此,编写一个依赖于此异常的程序是错误的:迭代器的快速失败行为应该只用于检测错误。

代码示例

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

@Override
 protected void verify(List<T> elements) {
  assertEquals(Sets.newHashSet(elements), Sets.newHashSet(mmHeap.iterator()));
  assertIntact(mmHeap);
 }
};

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

@Override
protected Iterator<T> newTargetIterator() {
 mmHeap = MinMaxPriorityQueue.create(values);
 return mmHeap.iterator();
}

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

public void testIteratorPastEndException() {
 MinMaxPriorityQueue<Integer> mmHeap = MinMaxPriorityQueue.create();
 mmHeap.addAll(Lists.newArrayList(1, 2));
 Iterator<Integer> it = mmHeap.iterator();
 assertTrue("Iterator has reached end prematurely", it.hasNext());
 it.next();
 it.next();
 try {
  it.next();
  fail("No exception thrown when iterating past end of heap");
 } catch (NoSuchElementException expected) {
 }
}

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

public void testIteratorConcurrentModification() {
 MinMaxPriorityQueue<Integer> mmHeap = MinMaxPriorityQueue.create();
 mmHeap.addAll(Lists.newArrayList(1, 2, 3, 4));
 Iterator<Integer> it = mmHeap.iterator();
 assertTrue("Iterator has reached end prematurely", it.hasNext());
 it.next();
 it.next();
 mmHeap.remove(4);
 try {
  it.next();
  fail("No exception thrown when iterating a modified heap");
 } catch (ConcurrentModificationException expected) {
 }
}

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

/** Tests a failure caused by fix to childless uncle issue. */
public void testIteratorRegressionChildlessUncle() {
 final ArrayList<Integer> initial = Lists.newArrayList(1, 15, 13, 8, 9, 10, 11, 14);
 MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create(initial);
 assertIntact(q);
 q.remove(9);
 q.remove(11);
 q.remove(10);
 // Now we're in the critical state: [1, 15, 13, 8, 14]
 // Removing 8 while iterating caused duplicates in iteration result.
 List<Integer> result = Lists.newArrayListWithCapacity(initial.size());
 for (Iterator<Integer> iter = q.iterator(); iter.hasNext(); ) {
  Integer value = iter.next();
  result.add(value);
  if (value == 8) {
   iter.remove();
  }
 }
 assertIntact(q);
 assertThat(result).containsExactly(1, 15, 13, 8, 14);
}

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

public void testRandomAddsAndRemoves_duplicateElements() {
 Random random = new Random(0);
 Multiset<Element> elements = HashMultiset.create();
 MinMaxPriorityQueue<Element> queue = MinMaxPriorityQueue.create();
 int range = Element.values().length;
 for (int iter = 0; iter < reduceIterationsIfGwt(1000); iter++) {
  for (int i = 0; i < 100; i++) {
   Element element = Element.values()[random.nextInt(range)];
   elements.add(element);
   queue.add(element);
  }
  Iterator<Element> queueIterator = queue.iterator();
  int remaining = queue.size();
  while (queueIterator.hasNext()) {
   Element element = queueIterator.next();
   remaining--;
   assertThat(elements).contains(element);
   if (random.nextBoolean()) {
    elements.remove(element);
    queueIterator.remove();
   }
  }
  assertThat(remaining).isEqualTo(0);
  assertIntact(queue);
  assertThat(queue).containsExactlyElementsIn(elements);
 }
}

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

Iterator<Pair<String, Integer>> qIterator = queue.iterator();
Set<String> topKDimensions = new HashSet<>();
topKDimensions.add("ALL");

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

Lists.newArrayList(1, 20, 1000, 2, 3, 30, 40, 10, 11, 12, 13, 200, 300, 500, 400));
assertTrue("Heap is not intact initially", mmHeap.isIntact());
Iterator<Integer> it = mmHeap.iterator();
assertEquals((Integer) 1, it.next());
assertEquals((Integer) 20, it.next());

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

assertEquals(7, mmHeap.size());
assertTrue("Heap is not intact initially", mmHeap.isIntact());
Iterator<Integer> it = mmHeap.iterator();
assertEquals((Integer) 1, it.next());
assertEquals((Integer) 20, it.next());

代码示例来源:origin: com.n3twork.druid/druid-processing

@Override
public Iterator<DimValHolder> getTopNIterator()
{
 return pQueue.iterator();
}

代码示例来源:origin: com.n3twork.druid/druid-processing

@Override
public Iterator<DimValHolder> getTopNIterator()
{
 return pQueue.iterator();
}

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

@Override protected Iterator<T> newTargetIterator() {
 mmHeap = MinMaxPriorityQueue.create(values);
 return mmHeap.iterator();
}
@Override protected void verify(List<T> elements) {

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

@Override protected void verify(List<T> elements) {
  assertEquals(Sets.newHashSet(elements),
    Sets.newHashSet(mmHeap.iterator()));
  assertTrue("Invalid MinMaxHeap: " + mmHeap, mmHeap.isIntact());
 }
};

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

public void testIteratorPastEndException() {
 MinMaxPriorityQueue<Integer> mmHeap = MinMaxPriorityQueue.create();
 mmHeap.addAll(Lists.newArrayList(1, 2));
 Iterator<Integer> it = mmHeap.iterator();
 assertTrue("Iterator has reached end prematurely", it.hasNext());
 it.next();
 it.next();
 try {
  it.next();
  fail("No exception thrown when iterating past end of heap");
 } catch (NoSuchElementException expected) {
 }
}

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

public void testIteratorConcurrentModification() {
 MinMaxPriorityQueue<Integer> mmHeap = MinMaxPriorityQueue.create();
 mmHeap.addAll(Lists.newArrayList(1, 2, 3, 4));
 Iterator<Integer> it = mmHeap.iterator();
 assertTrue("Iterator has reached end prematurely", it.hasNext());
 it.next();
 it.next();
 mmHeap.remove(4);
 try {
  it.next();
  fail("No exception thrown when iterating a modified heap");
 } catch (ConcurrentModificationException expected) {
 }
}

代码示例来源:origin: locationtech/geowave

iterators.add(queue.iterator());
return bestScenes.iterator();

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

public void testRandomAddsAndRemoves_duplicateElements() {
 Random random = new Random(0);
 Multiset<Element> elements = HashMultiset.create();
 MinMaxPriorityQueue<Element> queue = MinMaxPriorityQueue.create();
 int range = Element.values().length;
 for (int iter = 0; iter < 1000; iter++) {
  for (int i = 0; i < 100; i++) {
   Element element = Element.values()[random.nextInt(range)];
   elements.add(element);
   queue.add(element);
  }
  Iterator<Element> queueIterator = queue.iterator();
  int remaining = queue.size();
  while (queueIterator.hasNext()) {
   Element element = queueIterator.next();
   remaining--;
   assertThat(elements).contains(element);
   if (random.nextBoolean()) {
    elements.remove(element);
    queueIterator.remove();
   }
  }
  assertThat(remaining).isEqualTo(0);
  assertThat(queue.isIntact()).isTrue();
  assertThat(queue).containsExactlyElementsIn(elements);
 }
}

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

/**
 * Tests a failure caused by fix to childless uncle issue.
 */
public void testIteratorRegressionChildlessUncle() {
 final ArrayList<Integer> initial = Lists.newArrayList(
   1, 15, 13, 8, 9, 10, 11, 14);
 MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create(initial);
 assertTrue("State " + Arrays.toString(q.toArray()), q.isIntact());
 q.remove(9);
 q.remove(11);
 q.remove(10);
 // Now we're in the critical state: [1, 15, 13, 8, 14]
 // Removing 8 while iterating caused duplicates in iteration result.
 List<Integer> result = Lists.newArrayListWithCapacity(initial.size());
 for (Iterator<Integer> iter = q.iterator(); iter.hasNext();) {
  Integer value = iter.next();
  result.add(value);
  if (value == 8) {
   iter.remove();
  }
 }
 assertTrue(q.isIntact());
 assertThat(result).containsExactly(1, 15, 13, 8, 14);
}

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

1, 20, 1000, 2, 3, 30, 40, 10, 11, 12, 13, 200, 300, 500, 400));
assertTrue("Heap is not intact initially", mmHeap.isIntact());
Iterator<Integer> it = mmHeap.iterator();
assertEquals((Integer) 1, it.next());
assertEquals((Integer) 20, it.next());

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

assertEquals(7, mmHeap.size());
assertTrue("Heap is not intact initially", mmHeap.isIntact());
Iterator<Integer> it = mmHeap.iterator();
assertEquals((Integer) 1, it.next());
assertEquals((Integer) 20, it.next());

相关文章