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

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

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

Iterator介绍

[英]An iterator over a sequence of objects, such as a collection.

If a collection has been changed since the iterator was created, methods next and hasNext() may throw a ConcurrentModificationException. It is not possible to guarantee that this mechanism works in all cases of unsynchronized concurrent modification. It should only be used for debugging purposes. Iterators with this behavior are called fail-fast iterators.

Implementing Iterable and returning an Iterator allows your class to be used as a collection with the enhanced for loop.
[中]

代码示例

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

@Override
public T next() {
 if (!iterator.hasNext()) {
  iterator = iterable.iterator();
 }
 return iterator.next();
}

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

/** Clears the iterator using its remove method. */
static void clear(Iterator<?> iterator) {
 checkNotNull(iterator);
 while (iterator.hasNext()) {
  iterator.next();
  iterator.remove();
 }
}

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

@Override
public T next() {
 T next = iterator.next();
 iterator.remove();
 return next;
}

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

@Override
public boolean hasNext() {
  return sit.hasNext();
}

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

public void testCycleRemoveWithoutNext() {
 Iterator<String> cycle = Iterators.cycle("a", "b");
 assertTrue(cycle.hasNext());
 try {
  cycle.remove();
  fail("no exception thrown");
 } catch (IllegalStateException expected) {
 }
}

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

@Override
 public Object execute(Iterator<?> iterator) {
  iterator.remove();
  return null;
 }
};

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

@Override
  public Integer apply(Integer v) throws Exception {
    Observable.just(1).delay(10, TimeUnit.SECONDS).blockingIterable().iterator().next();
    return v;
  }
})

代码示例来源:origin: ReactiveX/RxJava

@Override
public final boolean isEmpty() {
  return it == null || !it.hasNext();
}

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

@Override
 public final void remove() {
  backingIterator.remove();
 }
}

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

@Override
 public boolean tryAdvance(Consumer<? super R> action) {
  if (itrA.hasNext() && itrB.hasNext()) {
   action.accept(function.apply(itrA.next(), itrB.next()));
   return true;
  }
  return false;
 }
},

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

private void assertIteratorIsUnmodifiable(Iterator<?> iterator) {
 iterator.next();
 try {
  iterator.remove();
  fail();
 } catch (UnsupportedOperationException expected) {
 }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void interrupt() {
  Iterator<Object> it = Observable.never().blockingNext().iterator();
  try {
    Thread.currentThread().interrupt();
    it.next();
  } catch (RuntimeException ex) {
    assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Integer apply(Integer v) throws Exception {
    Observable.just(1).delay(10, TimeUnit.SECONDS).blockingLatest().iterator().hasNext();
    return v;
  }
})

代码示例来源:origin: square/okhttp

@Override public void remove() {
  if (!canRemove) throw new IllegalStateException("remove() before next()");
  delegate.remove();
 }
};

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

@Override
public T next() {
 if (!iterator.hasNext()) {
  iterator = iterable.iterator();
  if (!iterator.hasNext()) {
   throw new NoSuchElementException();
  }
 }
 return iterator.next();
}

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

public void testCycleRemoveSameElementTwice() {
 Iterator<String> cycle = Iterators.cycle("a", "b");
 cycle.next();
 cycle.remove();
 try {
  cycle.remove();
  fail("no exception thrown");
 } catch (IllegalStateException expected) {
 }
}

相关文章