java.util.Collection.iterator()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(127)

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

Collection.iterator介绍

[英]Returns an instance of Iterator that may be used to access the objects contained by this Collection. The order in which the elements are returned by the iterator is not defined. Only if the instance of the Collection has a defined order the elements are returned in that order.
[中]返回可用于访问此集合包含的对象的迭代器实例。迭代器返回元素的顺序没有定义。仅当集合实例具有定义的顺序时,元素才会按该顺序返回。

代码示例

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

private ClusterException(Collection<? extends Throwable> exceptions) {
 super(
   exceptions.size() + " exceptions were thrown. The first exception is listed as a cause.",
   exceptions.iterator().next());
 ArrayList<Throwable> temp = new ArrayList<>();
 temp.addAll(exceptions);
 this.exceptions = Collections.unmodifiableCollection(temp);
}

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

private static <V extends Enum<V>> Class<V> inferValueType(Map<?, V> map) {
 if (map instanceof EnumBiMap) {
  return ((EnumBiMap<?, V>) map).valueType;
 }
 checkArgument(!map.isEmpty());
 return map.values().iterator().next().getDeclaringClass();
}

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

public void testIteratorNoSuchElementException() {
  Iterator<E> iterator = collection.iterator();
  while (iterator.hasNext()) {
   iterator.next();
  }

  try {
   iterator.next();
   fail("iterator.next() should throw NoSuchElementException");
  } catch (NoSuchElementException expected) {
  }
 }
}

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

public void clean() {
  // the lock protects removal from a concurrent put which could otherwise mutate the
  // queue after it has been removed from the map
  synchronized (unsent) {
    Iterator<ConcurrentLinkedQueue<ClientRequest>> iterator = unsent.values().iterator();
    while (iterator.hasNext()) {
      ConcurrentLinkedQueue<ClientRequest> requests = iterator.next();
      if (requests.isEmpty())
        iterator.remove();
    }
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

/** Returns the (first) root of this SemanticGraph. */
public IndexedWord getFirstRoot() {
 if (roots.isEmpty())
  throw new RuntimeException("No roots in graph:\n" + this
    + "\nFind where this graph was created and make sure you're adding roots.");
 return roots.iterator().next();
}

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

@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
 @CollectionSize.Require(ONE)
 public void testValuesIteratorRemove() {
  Iterator<V> valuesItr = multimap().values().iterator();
  valuesItr.next();
  valuesItr.remove();
  assertTrue(multimap().isEmpty());
 }
}

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

@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
@CollectionSize.Require(absent = ZERO)
public void testIterator_removeAffectsBackingCollection() {
 int originalSize = collection.size();
 Iterator<E> iterator = collection.iterator();
 Object element = iterator.next();
 // If it's an Entry, it may become invalid once it's removed from the Map. Copy it.
 if (element instanceof Entry) {
  Entry<?, ?> entry = (Entry<?, ?>) element;
  element = mapEntry(entry.getKey(), entry.getValue());
 }
 assertTrue(collection.contains(element)); // sanity check
 iterator.remove();
 assertFalse(collection.contains(element));
 assertEquals(originalSize - 1, collection.size());
}

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

private static <E> ArrayList<E> toArrayList(Collection<E> c) {
 // Avoid calling ArrayList(Collection), which may call back into toArray.
 ArrayList<E> result = new ArrayList<>(c.size());
 Iterators.addAll(result, c.iterator());
 return result;
}

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

private void assertCollectionSize(Collection<?> collection, int size) {
  assertEquals(size, collection.size());
  if (size > 0) {
   assertFalse(collection.isEmpty());
  } else {
   assertTrue(collection.isEmpty());
  }
  assertEquals(size, Iterables.size(collection));
  assertEquals(size, Iterators.size(collection.iterator()));
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Removes all elements in the given Collection that aren't accepted by the given Filter.
 */
public static <E> void retainAll(Collection<E> elems, Predicate<? super E> filter) {
 for (Iterator<E> iter = elems.iterator(); iter.hasNext();) {
  E elem = iter.next();
  if ( ! filter.test(elem)) {
   iter.remove();
  }
 }
}

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

public TestBean getTestBean() {
    return this.testBeanFactory.get().values().iterator().next();
  }
}

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

/**
 * Collect any {@link AsyncConfigurer} beans through autowiring.
 */
@Autowired(required = false)
void setConfigurers(Collection<AsyncConfigurer> configurers) {
  if (CollectionUtils.isEmpty(configurers)) {
    return;
  }
  if (configurers.size() > 1) {
    throw new IllegalStateException("Only one AsyncConfigurer may exist");
  }
  AsyncConfigurer configurer = configurers.iterator().next();
  this.executor = configurer::getAsyncExecutor;
  this.exceptionHandler = configurer::getAsyncUncaughtExceptionHandler;
}

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

@Override
public final int getTransactionIsolation() throws SQLException {
  if (cachedConnections.values().isEmpty()) {
    return transactionIsolation;
  }
  return cachedConnections.values().iterator().next().getTransactionIsolation();
}

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

@Override
public String toString() {
  StringBuilder builder = new StringBuilder("[");
  for (Iterator<?> iterator = getContent().iterator(); iterator.hasNext();) {
    Object expression = iterator.next();
    builder.append(expression.toString());
    if (iterator.hasNext()) {
      builder.append(getToStringInfix());
    }
  }
  builder.append("]");
  return builder.toString();
}

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

@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
@CollectionSize.Require(absent = ZERO)
public void testIteratorRemove_unsupported() {
 Iterator<E> iterator = collection.iterator();
 iterator.next();
 try {
  iterator.remove();
  fail("iterator.remove() should throw UnsupportedOperationException");
 } catch (UnsupportedOperationException expected) {
 }
 expectUnchanged();
 assertTrue(collection.contains(e0()));
}

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

private static <E> ArrayList<E> toArrayList(Collection<E> c) {
 // Avoid calling ArrayList(Collection), which may call back into toArray.
 ArrayList<E> result = new ArrayList<E>(c.size());
 Iterators.addAll(result, c.iterator());
 return result;
}

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

private String style(Collection<?> value) {
  StringBuilder result = new StringBuilder(value.size() * 8 + 16);
  result.append(getCollectionTypeString(value)).append('[');
  for (Iterator<?> i = value.iterator(); i.hasNext();) {
    result.append(style(i.next()));
    if (i.hasNext()) {
      result.append(',').append(' ');
    }
  }
  if (value.isEmpty()) {
    result.append(EMPTY);
  }
  result.append("]");
  return result.toString();
}

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

public TestBean getTestBean() {
    return this.testBeanFactory.get().values().iterator().next();
  }
}

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

@Autowired(required = false)
void setConfigurers(Collection<TransactionManagementConfigurer> configurers) {
  if (CollectionUtils.isEmpty(configurers)) {
    return;
  }
  if (configurers.size() > 1) {
    throw new IllegalStateException("Only one TransactionManagementConfigurer may exist");
  }
  TransactionManagementConfigurer configurer = configurers.iterator().next();
  this.txManager = configurer.annotationDrivenTransactionManager();
}

代码示例来源:origin: eclipse-vertx/vert.x

public static <T> T loadFactoryOrNull(Class<T> clazz) {
 Collection<T> collection = loadFactories(clazz);
 if (!collection.isEmpty()) {
  return collection.iterator().next();
 } else {
  return null;
 }
}

相关文章