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

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

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

Iterator.remove介绍

[英]Removes the last object returned by next from the collection. This method can only be called once between each call to next.
[中]从集合中删除next返回的最后一个对象。此方法只能在每次调用下一次之间调用一次。

代码示例

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

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

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

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);
}

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

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 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: 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: 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

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

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

@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;
}

代码示例来源:origin: redisson/redisson

@Override
  public void accept(List<T> bucket) {
    Iterator<List<T>> it = buckets.iterator();
    while (it.hasNext()) {
      List<T> itBucket = it.next();
      if (bucket == itBucket) {
        it.remove();
        broadcastNext(bucket);
        break;
      }
    }
  }
};

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

Iterator<Map.Entry<String,String>> iter = TestMap.entrySet().iterator();
while (iter.hasNext()) {
  Map.Entry<String,String> entry = iter.next();
  if("Sample".equalsIgnoreCase(entry.getValue())){
    iter.remove();
  }
}

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

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

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

@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;
}

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

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);
}

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

/**
 * Deletes and returns the next value from the iterator, or returns {@code null} if there is no
 * such value.
 */
static <T> @Nullable T pollNext(Iterator<T> iterator) {
 if (iterator.hasNext()) {
  T result = iterator.next();
  iterator.remove();
  return result;
 } else {
  return null;
 }
}

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

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

相关文章