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(); } }
@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(); }
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(); } } }
@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 } }
@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(); }
/** * 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); }