java.util.Iterator.next()方法的使用及代码示例

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

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

Iterator.next介绍

[英]Returns the next object and advances the iterator.
[中]返回下一个对象并推进迭代器。

代码示例

代码示例来源:origin: stackoverflow.com

public static void printMap(Map mp) {
  Iterator it = mp.entrySet().iterator();
  while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    System.out.println(pair.getKey() + " = " + pair.getValue());
    it.remove(); // avoids a ConcurrentModificationException
  }
}

代码示例来源:origin: stackoverflow.com

List<String> list = new ArrayList<>();

// This is a clever way to create the iterator and call iterator.hasNext() like
// you would do in a while-loop. It would be the same as doing:
//     Iterator<String> iterator = list.iterator();
//     while (iterator.hasNext()) {
for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
  String string = iterator.next();
  if (string.isEmpty()) {
    // Remove the current element from the iterator and the list.
    iterator.remove();
  }
}

代码示例来源:origin: iluwatar/java-design-patterns

private void processPendingCommands() {
 Iterator<Runnable> iterator = pendingCommands.iterator();
 while (iterator.hasNext()) {
  Runnable command = iterator.next();
  command.run();
  iterator.remove();
 }
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
  * Collects the remaining objects of the given iterator into a List.
  * 
  * @return a new List with the remaining objects.
  */
 public static <E> List<E> toList(Iterator<E> iterator) {
  List<E> copy = new ArrayList<>();
  while (iterator.hasNext()) {
   copy.add(iterator.next());
  }
  return copy;
 }
}

代码示例来源:origin: stackoverflow.com

Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
  Map.Entry entry = (Map.Entry) entries.next();
  Integer key = (Integer)entry.getKey();
  Integer value = (Integer)entry.getValue();
  System.out.println("Key = " + key + ", Value = " + value);
}

代码示例来源: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: spring-projects/spring-framework

@Override
public String getPath() {
  StringBuilder pathBuilder = new StringBuilder();
  pathBuilder.append(PATH_DELIMITER);
  for (Iterator<String> iterator = this.pathSegments.iterator(); iterator.hasNext(); ) {
    String pathSegment = iterator.next();
    pathBuilder.append(pathSegment);
    if (iterator.hasNext()) {
      pathBuilder.append(PATH_DELIMITER);
    }
  }
  return pathBuilder.toString();
}

代码示例来源: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: google/guava

public void testAdvance_basic() {
 List<String> list = newArrayList();
 list.add("a");
 list.add("b");
 Iterator<String> iterator = list.iterator();
 advance(iterator, 1);
 assertEquals("b", iterator.next());
}

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

@CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION)
@ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
public void testAddAtIndexConcurrentWithIteration() {
 try {
  Iterator<E> iterator = collection.iterator();
  getList().add(0, e3());
  iterator.next();
  fail("Expected ConcurrentModificationException");
 } catch (ConcurrentModificationException expected) {
  // success
 }
}

代码示例来源: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: iluwatar/java-design-patterns

/**
 * Checkout object from pool
 */
public synchronized T checkOut() {
 if (available.isEmpty()) {
  available.add(create());
 }
 T instance = available.iterator().next();
 available.remove(instance);
 inUse.add(instance);
 return instance;
}

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

public void testSize_partiallyConsumed() {
 Iterator<Integer> iterator = asList(1, 2, 3, 4, 5).iterator();
 iterator.next();
 iterator.next();
 assertEquals(3, Iterators.size(iterator));
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
 * @return the count of remaining objects of the current Iterable
 */
public final int getRemainingElementsCount() {
 int counter = 0;
 Iterator<E> iterator = iterator();
 while (iterator.hasNext()) {
  iterator.next();
  counter++;
 }
 return counter;
}

代码示例来源: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: ReactiveX/RxJava

@Test(expected = UnsupportedOperationException.class)
public void toFlowableIterableRemove() {
  @SuppressWarnings("unchecked")
  Iterable<? extends Flowable<Integer>> f = SingleInternalHelper.iterableToFlowable(Arrays.asList(Single.just(1)));
  Iterator<? extends Flowable<Integer>> iterator = f.iterator();
  iterator.next();
  iterator.remove();
}

代码示例来源:origin: skylot/jadx

public static void remove(List<InsnNode> list, InsnNode insn) {
  for (Iterator<InsnNode> iterator = list.iterator(); iterator.hasNext(); ) {
    InsnNode next = iterator.next();
    if (next == insn) {
      iterator.remove();
      return;
    }
  }
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
 * Transforms this FluentIterable into a new one containing objects of the type T.
 * 
 * @param function a function that transforms an instance of E into an instance of T
 * @param <T> the target type of the transformation
 * @return a new FluentIterable of the new type
 */
@Override
public final <T> FluentIterable<T> map(Function<? super E, T> function) {
 List<T> temporaryList = new ArrayList<>();
 Iterator<E> iterator = iterator();
 while (iterator.hasNext()) {
  temporaryList.add(function.apply(iterator.next()));
 }
 return from(temporaryList);
}

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

@Override
public boolean remove(Object o) {
 Iterator<Entry<K, V>> entryItr = unfiltered.entrySet().iterator();
 while (entryItr.hasNext()) {
  Entry<K, V> entry = entryItr.next();
  if (predicate.apply(entry) && Objects.equal(entry.getValue(), o)) {
   entryItr.remove();
   return true;
  }
 }
 return false;
}

代码示例来源:origin: stackoverflow.com

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
  Map.Entry<Integer, Integer> entry = entries.next();
  System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

相关文章