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(); } }
@CollectionSize.Require(ONE) @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) public void testEntrySetIteratorRemove() { Set<Entry<K, V>> entrySet = getMap().entrySet(); Iterator<Entry<K, V>> entryItr = entrySet.iterator(); assertEquals(e0(), entryItr.next()); entryItr.remove(); assertTrue(getMap().isEmpty()); assertFalse(entrySet.contains(e0())); }
public void testTransformRemove() { List<String> list = Lists.newArrayList("1", "2", "3"); Iterator<String> input = list.iterator(); Iterator<Integer> iterator = Iterators.transform( input, new Function<String, Integer>() { @Override public Integer apply(String from) { return Integer.valueOf(from); } }); assertEquals(Integer.valueOf(1), iterator.next()); assertEquals(Integer.valueOf(2), iterator.next()); iterator.remove(); assertEquals(asList("1", "3"), list); }
@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()); }
@CollectionSize.Require(SEVERAL) @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) public void testAsMapEntrySetIteratorRemovePropagatesToMultimap() { resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3())); Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet(); Iterator<Entry<K, Collection<V>>> asMapEntryItr = asMapEntrySet.iterator(); asMapEntryItr.next(); asMapEntryItr.remove(); assertTrue(multimap().isEmpty()); } }
@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(); }
@Override public boolean retainAll(Collection<?> collection) { Iterator<Entry<K, V>> entryItr = unfiltered.entrySet().iterator(); boolean result = false; while (entryItr.hasNext()) { Entry<K, V> entry = entryItr.next(); if (predicate.apply(entry) && !collection.contains(entry.getValue())) { entryItr.remove(); result = true; } } return result; }
public void testIdentityKeySetIteratorRemove() { BiMap<Integer, String> bimap = new AbstractBiMap<Integer, String>( new IdentityHashMap<Integer, String>(), new IdentityHashMap<String, Integer>()) {}; bimap.put(1, "one"); bimap.put(2, "two"); bimap.put(3, "three"); Iterator<Integer> iterator = bimap.keySet().iterator(); iterator.next(); iterator.next(); iterator.remove(); iterator.next(); iterator.remove(); assertEquals(1, bimap.size()); assertEquals(1, bimap.inverse().size()); }
@Override public boolean removeAll(Collection<?> collection) { Iterator<Entry<K, V>> entryItr = unfiltered.entrySet().iterator(); boolean result = false; while (entryItr.hasNext()) { Entry<K, V> entry = entryItr.next(); if (predicate.apply(entry) && collection.contains(entry.getValue())) { entryItr.remove(); result = true; } } return result; }
private static void removeInsn(MethodNode mth, BlockNode block, PhiInsn phiInsn) { Iterator<InsnNode> it = block.getInstructions().iterator(); while (it.hasNext()) { InsnNode insn = it.next(); if (insn == phiInsn) { it.remove(); return; } } LOG.warn("Phi node not removed: {}, mth: {}", phiInsn, mth); }
public void testBiMapEntrySetIteratorRemove() { BiMap<Integer, String> map = HashBiMap.create(); map.put(1, "one"); Set<Entry<Integer, String>> entries = map.entrySet(); Iterator<Entry<Integer, String>> iterator = entries.iterator(); Entry<Integer, String> entry = iterator.next(); entry.setValue("two"); // changes the iterator's current entry value assertEquals("two", map.get(1)); assertEquals(Integer.valueOf(1), map.inverse().get("two")); iterator.remove(); // removes the updated entry assertTrue(map.isEmpty()); }