java.util.Collection.clear()方法的使用及代码示例

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

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

Collection.clear介绍

[英]Removes all elements from this Collection, leaving it empty (optional).
[中]删除此集合中的所有元素,将其保留为空(可选)。

代码示例

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

  1. public void setDynamicRecords( Collection<DynamicRecord> records )
  2. {
  3. this.records.clear();
  4. this.records.addAll( records );
  5. }

代码示例来源:origin: stanfordnlp/CoreNLP

  1. public void setRoot(IndexedWord word) {
  2. roots.clear();
  3. roots.add(word);
  4. }

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

  1. /** Removes all values for the provided key. */
  2. private void removeValuesForKey(Object key) {
  3. Collection<V> collection = Maps.safeRemove(map, key);
  4. if (collection != null) {
  5. int count = collection.size();
  6. collection.clear();
  7. totalSize -= count;
  8. }
  9. }

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

  1. boolean removeEntriesIf(Predicate<? super Entry<K, Collection<V>>> predicate) {
  2. Iterator<Entry<K, Collection<V>>> entryIterator = unfiltered.asMap().entrySet().iterator();
  3. boolean changed = false;
  4. while (entryIterator.hasNext()) {
  5. Entry<K, Collection<V>> entry = entryIterator.next();
  6. K key = entry.getKey();
  7. Collection<V> collection = filterCollection(entry.getValue(), new ValuePredicate(key));
  8. if (!collection.isEmpty() && predicate.apply(Maps.immutableEntry(key, collection))) {
  9. if (collection.size() == entry.getValue().size()) {
  10. entryIterator.remove();
  11. } else {
  12. collection.clear();
  13. }
  14. changed = true;
  15. }
  16. }
  17. return changed;
  18. }

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

  1. @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
  2. @CollectionSize.Require(SEVERAL)
  3. public void testClearConcurrentWithIteration() {
  4. try {
  5. Iterator<E> iterator = collection.iterator();
  6. collection.clear();
  7. iterator.next();
  8. /*
  9. * We prefer for iterators to fail immediately on hasNext, but ArrayList
  10. * and LinkedList will notably return true on hasNext here!
  11. */
  12. fail("Expected ConcurrentModificationException");
  13. } catch (ConcurrentModificationException expected) {
  14. // success
  15. }
  16. }
  17. }

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

  1. public void handleMembershipChange(Collection<Request> requests) {
  2. Collection<Address> leaving_mbrs=new LinkedHashSet<>(requests.size());
  3. requests.forEach(r -> {
  4. if(r.type == Request.SUSPECT)
  5. suspected_mbrs.add(r.mbr);
  6. else if(r.type == Request.LEAVE)
  7. leaving_mbrs.add(r.mbr);
  8. });
  9. if(suspected_mbrs.isEmpty() && leaving_mbrs.isEmpty())
  10. return;
  11. if(wouldIBeCoordinator(leaving_mbrs)) {
  12. log.debug("%s: members are %s, coord=%s: I'm the new coordinator", gms.local_addr, gms.members, gms.local_addr);
  13. gms.becomeCoordinator();
  14. Collection<Request> leavingOrSuspectedMembers=new LinkedHashSet<>();
  15. leaving_mbrs.forEach(mbr -> leavingOrSuspectedMembers.add(new Request(Request.LEAVE, mbr)));
  16. suspected_mbrs.forEach(mbr -> {
  17. leavingOrSuspectedMembers.add(new Request(Request.SUSPECT, mbr));
  18. gms.ack_collector.suspect(mbr);
  19. });
  20. suspected_mbrs.clear();
  21. gms.getViewHandler().add(leavingOrSuspectedMembers);
  22. }
  23. }

代码示例来源:origin: commons-collections/commons-collections

  1. /**
  2. * Clear the map.
  3. * <p>
  4. * This clears each collection in the map, and so may be slow.
  5. */
  6. public void clear() {
  7. // For gc, clear each list in the map
  8. Set pairs = super.entrySet();
  9. Iterator pairsIterator = pairs.iterator();
  10. while (pairsIterator.hasNext()) {
  11. Map.Entry keyValuePair = (Map.Entry) pairsIterator.next();
  12. Collection coll = (Collection) keyValuePair.getValue();
  13. coll.clear();
  14. }
  15. super.clear();
  16. }

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

  1. @CollectionFeature.Require(SUPPORTS_REMOVE)
  2. public void testClear() {
  3. collection.clear();
  4. assertTrue("After clear(), a collection should be empty.", collection.isEmpty());
  5. assertEquals(0, collection.size());
  6. assertFalse(collection.iterator().hasNext());
  7. }

代码示例来源:origin: Bilibili/DanmakuFlameMaster

  1. public void setItems(Collection<BaseDanmaku> items) {
  2. if (mDuplicateMergingEnabled && mSortType != ST_BY_LIST) {
  3. synchronized (this.mLockObject) {
  4. this.items.clear();
  5. this.items.addAll(items);
  6. items = this.items;
  7. }
  8. } else {
  9. this.items = items;
  10. }
  11. if (items instanceof List) {
  12. mSortType = ST_BY_LIST;
  13. }
  14. mSize.set(items == null ? 0 : items.size());
  15. }

代码示例来源:origin: commons-collections/commons-collections

  1. /**
  2. * Tests {@link Set#equals(Object)}.
  3. */
  4. public void testSetEquals() {
  5. resetEmpty();
  6. assertEquals("Empty sets should be equal",
  7. getSet(), getConfirmedSet());
  8. verify();
  9. Collection set2 = makeConfirmedCollection();
  10. set2.add("foo");
  11. assertTrue("Empty set shouldn't equal nonempty set",
  12. !getSet().equals(set2));
  13. resetFull();
  14. assertEquals("Full sets should be equal", getSet(), getConfirmedSet());
  15. verify();
  16. set2.clear();
  17. set2.addAll(Arrays.asList(getOtherElements()));
  18. assertTrue("Sets with different contents shouldn't be equal",
  19. !getSet().equals(set2));
  20. }

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

  1. /**
  2. * {@inheritDoc}
  3. *
  4. * <p>The returned collection is immutable.
  5. */
  6. @Override
  7. public Collection<V> replaceValues(@Nullable K key, Iterable<? extends V> values) {
  8. Iterator<? extends V> iterator = values.iterator();
  9. if (!iterator.hasNext()) {
  10. return removeAll(key);
  11. }
  12. // TODO(lowasser): investigate atomic failure?
  13. Collection<V> collection = getOrCreateCollection(key);
  14. Collection<V> oldValues = createCollection();
  15. oldValues.addAll(collection);
  16. totalSize -= collection.size();
  17. collection.clear();
  18. while (iterator.hasNext()) {
  19. if (collection.add(iterator.next())) {
  20. totalSize++;
  21. }
  22. }
  23. return unmodifiableCollectionSubclass(oldValues);
  24. }

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

  1. Collection<V> values = multimap.asMap().entrySet().iterator().next().getValue();
  2. K key = multimap.keySet().iterator().next();
  3. assertCollectionIsUnmodifiable(multimap.get(key), sampleValue);
  4. assertMultimapRemainsUnmodified(multimap, originalEntries);
  5. K presentKey = multimap.keySet().iterator().next();
  6. try {
  7. multimap.asMap().get(presentKey).remove(sampleValue);
  8. multimap.asMap().values().iterator().next().remove(sampleValue);
  9. fail("asMap().values().iterator().next().remove() succeeded on unmodifiable multimap");
  10. } catch (UnsupportedOperationException expected) {
  11. ((Collection<?>) multimap.asMap().values().toArray()[0]).clear();
  12. fail("asMap().values().toArray()[0].clear() succeeded on unmodifiable multimap");
  13. } catch (UnsupportedOperationException expected) {

代码示例来源:origin: org.apache.commons/commons-collections4

  1. /**
  2. * Factory method to create a transforming collection that will transform
  3. * existing contents of the specified collection.
  4. * <p>
  5. * If there are any elements already in the collection being decorated, they
  6. * will be transformed by this method.
  7. * Contrast this with {@link #transformingCollection(Collection, Transformer)}.
  8. *
  9. * @param <E> the type of the elements in the collection
  10. * @param collection the collection to decorate, must not be null
  11. * @param transformer the transformer to use for conversion, must not be null
  12. * @return a new transformed Collection
  13. * @throws NullPointerException if collection or transformer is null
  14. * @since 4.0
  15. */
  16. public static <E> TransformedCollection<E> transformedCollection(final Collection<E> collection,
  17. final Transformer<? super E, ? extends E> transformer) {
  18. final TransformedCollection<E> decorated = new TransformedCollection<>(collection, transformer);
  19. // null collection & transformer are disallowed by the constructor call above
  20. if (collection.size() > 0) {
  21. @SuppressWarnings("unchecked") // collection is of type E
  22. final E[] values = (E[]) collection.toArray(); // NOPMD - false positive for generics
  23. collection.clear();
  24. for (final E value : values) {
  25. decorated.decorated().add(transformer.transform(value));
  26. }
  27. }
  28. return decorated;
  29. }

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

  1. /**
  2. * Clear the map.
  3. * <p>
  4. * This clears each collection in the map, and so may be slow.
  5. */
  6. public void clear() {
  7. // For gc, clear each list in the map
  8. Set pairs = super.entrySet();
  9. Iterator pairsIterator = pairs.iterator();
  10. while (pairsIterator.hasNext()) {
  11. Map.Entry keyValuePair = (Map.Entry) pairsIterator.next();
  12. Collection coll = (Collection) keyValuePair.getValue();
  13. coll.clear();
  14. }
  15. super.clear();
  16. }

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

  1. /**
  2. * {@inheritDoc}
  3. *
  4. * <p>The returned collection is immutable.
  5. */
  6. @Override
  7. public Collection<V> removeAll(@Nullable Object key) {
  8. Collection<V> collection = map.remove(key);
  9. if (collection == null) {
  10. return createUnmodifiableEmptyCollection();
  11. }
  12. Collection<V> output = createCollection();
  13. output.addAll(collection);
  14. totalSize -= collection.size();
  15. collection.clear();
  16. return unmodifiableCollectionSubclass(output);
  17. }

代码示例来源:origin: apache/incubator-shardingsphere

  1. private Collection<SQLException> closeResultSets() {
  2. Collection<SQLException> result = new LinkedList<>();
  3. for (ResultSet each : cachedResultSets) {
  4. try {
  5. each.close();
  6. } catch (final SQLException ex) {
  7. result.add(ex);
  8. }
  9. }
  10. cachedResultSets.clear();
  11. return result;
  12. }

代码示例来源:origin: prestodb/presto

  1. /** Removes all values for the provided key. */
  2. private void removeValuesForKey(Object key) {
  3. Collection<V> collection = Maps.safeRemove(map, key);
  4. if (collection != null) {
  5. int count = collection.size();
  6. collection.clear();
  7. totalSize -= count;
  8. }
  9. }

代码示例来源:origin: stanfordnlp/CoreNLP

  1. public void setRoots(Collection<IndexedWord> words) {
  2. roots.clear();
  3. roots.addAll(words);
  4. }

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

  1. boolean removeEntriesIf(Predicate<? super Entry<K, Collection<V>>> predicate) {
  2. Iterator<Entry<K, Collection<V>>> entryIterator = unfiltered.asMap().entrySet().iterator();
  3. boolean changed = false;
  4. while (entryIterator.hasNext()) {
  5. Entry<K, Collection<V>> entry = entryIterator.next();
  6. K key = entry.getKey();
  7. Collection<V> collection = filterCollection(entry.getValue(), new ValuePredicate(key));
  8. if (!collection.isEmpty() && predicate.apply(Maps.immutableEntry(key, collection))) {
  9. if (collection.size() == entry.getValue().size()) {
  10. entryIterator.remove();
  11. } else {
  12. collection.clear();
  13. }
  14. changed = true;
  15. }
  16. }
  17. return changed;
  18. }

代码示例来源:origin: commons-collections/commons-collections

  1. collection.clear();
  2. fail("clear should raise UnsupportedOperationException");
  3. } catch (UnsupportedOperationException e) {
  4. Iterator iterator = collection.iterator();
  5. iterator.next();
  6. iterator.remove();
  7. fail("iterator.remove should raise UnsupportedOperationException");

相关文章