本文整理了Java中java.util.Collection.clear()
方法的一些代码示例,展示了Collection.clear()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Collection.clear()
方法的具体详情如下:
包路径:java.util.Collection
类名称:Collection
方法名:clear
[英]Removes all elements from this Collection, leaving it empty (optional).
[中]删除此集合中的所有元素,将其保留为空(可选)。
代码示例来源:origin: neo4j/neo4j
public void setDynamicRecords( Collection<DynamicRecord> records )
{
this.records.clear();
this.records.addAll( records );
}
代码示例来源:origin: stanfordnlp/CoreNLP
public void setRoot(IndexedWord word) {
roots.clear();
roots.add(word);
}
代码示例来源:origin: google/guava
/** Removes all values for the provided key. */
private void removeValuesForKey(Object key) {
Collection<V> collection = Maps.safeRemove(map, key);
if (collection != null) {
int count = collection.size();
collection.clear();
totalSize -= count;
}
}
代码示例来源:origin: google/guava
boolean removeEntriesIf(Predicate<? super Entry<K, Collection<V>>> predicate) {
Iterator<Entry<K, Collection<V>>> entryIterator = unfiltered.asMap().entrySet().iterator();
boolean changed = false;
while (entryIterator.hasNext()) {
Entry<K, Collection<V>> entry = entryIterator.next();
K key = entry.getKey();
Collection<V> collection = filterCollection(entry.getValue(), new ValuePredicate(key));
if (!collection.isEmpty() && predicate.apply(Maps.immutableEntry(key, collection))) {
if (collection.size() == entry.getValue().size()) {
entryIterator.remove();
} else {
collection.clear();
}
changed = true;
}
}
return changed;
}
代码示例来源:origin: google/guava
@CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
@CollectionSize.Require(SEVERAL)
public void testClearConcurrentWithIteration() {
try {
Iterator<E> iterator = collection.iterator();
collection.clear();
iterator.next();
/*
* We prefer for iterators to fail immediately on hasNext, but ArrayList
* and LinkedList will notably return true on hasNext here!
*/
fail("Expected ConcurrentModificationException");
} catch (ConcurrentModificationException expected) {
// success
}
}
}
代码示例来源:origin: wildfly/wildfly
public void handleMembershipChange(Collection<Request> requests) {
Collection<Address> leaving_mbrs=new LinkedHashSet<>(requests.size());
requests.forEach(r -> {
if(r.type == Request.SUSPECT)
suspected_mbrs.add(r.mbr);
else if(r.type == Request.LEAVE)
leaving_mbrs.add(r.mbr);
});
if(suspected_mbrs.isEmpty() && leaving_mbrs.isEmpty())
return;
if(wouldIBeCoordinator(leaving_mbrs)) {
log.debug("%s: members are %s, coord=%s: I'm the new coordinator", gms.local_addr, gms.members, gms.local_addr);
gms.becomeCoordinator();
Collection<Request> leavingOrSuspectedMembers=new LinkedHashSet<>();
leaving_mbrs.forEach(mbr -> leavingOrSuspectedMembers.add(new Request(Request.LEAVE, mbr)));
suspected_mbrs.forEach(mbr -> {
leavingOrSuspectedMembers.add(new Request(Request.SUSPECT, mbr));
gms.ack_collector.suspect(mbr);
});
suspected_mbrs.clear();
gms.getViewHandler().add(leavingOrSuspectedMembers);
}
}
代码示例来源:origin: commons-collections/commons-collections
/**
* Clear the map.
* <p>
* This clears each collection in the map, and so may be slow.
*/
public void clear() {
// For gc, clear each list in the map
Set pairs = super.entrySet();
Iterator pairsIterator = pairs.iterator();
while (pairsIterator.hasNext()) {
Map.Entry keyValuePair = (Map.Entry) pairsIterator.next();
Collection coll = (Collection) keyValuePair.getValue();
coll.clear();
}
super.clear();
}
代码示例来源:origin: google/guava
@CollectionFeature.Require(SUPPORTS_REMOVE)
public void testClear() {
collection.clear();
assertTrue("After clear(), a collection should be empty.", collection.isEmpty());
assertEquals(0, collection.size());
assertFalse(collection.iterator().hasNext());
}
代码示例来源:origin: Bilibili/DanmakuFlameMaster
public void setItems(Collection<BaseDanmaku> items) {
if (mDuplicateMergingEnabled && mSortType != ST_BY_LIST) {
synchronized (this.mLockObject) {
this.items.clear();
this.items.addAll(items);
items = this.items;
}
} else {
this.items = items;
}
if (items instanceof List) {
mSortType = ST_BY_LIST;
}
mSize.set(items == null ? 0 : items.size());
}
代码示例来源:origin: commons-collections/commons-collections
/**
* Tests {@link Set#equals(Object)}.
*/
public void testSetEquals() {
resetEmpty();
assertEquals("Empty sets should be equal",
getSet(), getConfirmedSet());
verify();
Collection set2 = makeConfirmedCollection();
set2.add("foo");
assertTrue("Empty set shouldn't equal nonempty set",
!getSet().equals(set2));
resetFull();
assertEquals("Full sets should be equal", getSet(), getConfirmedSet());
verify();
set2.clear();
set2.addAll(Arrays.asList(getOtherElements()));
assertTrue("Sets with different contents shouldn't be equal",
!getSet().equals(set2));
}
代码示例来源:origin: google/guava
/**
* {@inheritDoc}
*
* <p>The returned collection is immutable.
*/
@Override
public Collection<V> replaceValues(@Nullable K key, Iterable<? extends V> values) {
Iterator<? extends V> iterator = values.iterator();
if (!iterator.hasNext()) {
return removeAll(key);
}
// TODO(lowasser): investigate atomic failure?
Collection<V> collection = getOrCreateCollection(key);
Collection<V> oldValues = createCollection();
oldValues.addAll(collection);
totalSize -= collection.size();
collection.clear();
while (iterator.hasNext()) {
if (collection.add(iterator.next())) {
totalSize++;
}
}
return unmodifiableCollectionSubclass(oldValues);
}
代码示例来源:origin: google/guava
Collection<V> values = multimap.asMap().entrySet().iterator().next().getValue();
K key = multimap.keySet().iterator().next();
assertCollectionIsUnmodifiable(multimap.get(key), sampleValue);
assertMultimapRemainsUnmodified(multimap, originalEntries);
K presentKey = multimap.keySet().iterator().next();
try {
multimap.asMap().get(presentKey).remove(sampleValue);
multimap.asMap().values().iterator().next().remove(sampleValue);
fail("asMap().values().iterator().next().remove() succeeded on unmodifiable multimap");
} catch (UnsupportedOperationException expected) {
((Collection<?>) multimap.asMap().values().toArray()[0]).clear();
fail("asMap().values().toArray()[0].clear() succeeded on unmodifiable multimap");
} catch (UnsupportedOperationException expected) {
代码示例来源:origin: org.apache.commons/commons-collections4
/**
* Factory method to create a transforming collection that will transform
* existing contents of the specified collection.
* <p>
* If there are any elements already in the collection being decorated, they
* will be transformed by this method.
* Contrast this with {@link #transformingCollection(Collection, Transformer)}.
*
* @param <E> the type of the elements in the collection
* @param collection the collection to decorate, must not be null
* @param transformer the transformer to use for conversion, must not be null
* @return a new transformed Collection
* @throws NullPointerException if collection or transformer is null
* @since 4.0
*/
public static <E> TransformedCollection<E> transformedCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer) {
final TransformedCollection<E> decorated = new TransformedCollection<>(collection, transformer);
// null collection & transformer are disallowed by the constructor call above
if (collection.size() > 0) {
@SuppressWarnings("unchecked") // collection is of type E
final E[] values = (E[]) collection.toArray(); // NOPMD - false positive for generics
collection.clear();
for (final E value : values) {
decorated.decorated().add(transformer.transform(value));
}
}
return decorated;
}
代码示例来源:origin: wildfly/wildfly
/**
* Clear the map.
* <p>
* This clears each collection in the map, and so may be slow.
*/
public void clear() {
// For gc, clear each list in the map
Set pairs = super.entrySet();
Iterator pairsIterator = pairs.iterator();
while (pairsIterator.hasNext()) {
Map.Entry keyValuePair = (Map.Entry) pairsIterator.next();
Collection coll = (Collection) keyValuePair.getValue();
coll.clear();
}
super.clear();
}
代码示例来源:origin: google/guava
/**
* {@inheritDoc}
*
* <p>The returned collection is immutable.
*/
@Override
public Collection<V> removeAll(@Nullable Object key) {
Collection<V> collection = map.remove(key);
if (collection == null) {
return createUnmodifiableEmptyCollection();
}
Collection<V> output = createCollection();
output.addAll(collection);
totalSize -= collection.size();
collection.clear();
return unmodifiableCollectionSubclass(output);
}
代码示例来源:origin: apache/incubator-shardingsphere
private Collection<SQLException> closeResultSets() {
Collection<SQLException> result = new LinkedList<>();
for (ResultSet each : cachedResultSets) {
try {
each.close();
} catch (final SQLException ex) {
result.add(ex);
}
}
cachedResultSets.clear();
return result;
}
代码示例来源:origin: prestodb/presto
/** Removes all values for the provided key. */
private void removeValuesForKey(Object key) {
Collection<V> collection = Maps.safeRemove(map, key);
if (collection != null) {
int count = collection.size();
collection.clear();
totalSize -= count;
}
}
代码示例来源:origin: stanfordnlp/CoreNLP
public void setRoots(Collection<IndexedWord> words) {
roots.clear();
roots.addAll(words);
}
代码示例来源:origin: google/j2objc
boolean removeEntriesIf(Predicate<? super Entry<K, Collection<V>>> predicate) {
Iterator<Entry<K, Collection<V>>> entryIterator = unfiltered.asMap().entrySet().iterator();
boolean changed = false;
while (entryIterator.hasNext()) {
Entry<K, Collection<V>> entry = entryIterator.next();
K key = entry.getKey();
Collection<V> collection = filterCollection(entry.getValue(), new ValuePredicate(key));
if (!collection.isEmpty() && predicate.apply(Maps.immutableEntry(key, collection))) {
if (collection.size() == entry.getValue().size()) {
entryIterator.remove();
} else {
collection.clear();
}
changed = true;
}
}
return changed;
}
代码示例来源:origin: commons-collections/commons-collections
collection.clear();
fail("clear should raise UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
Iterator iterator = collection.iterator();
iterator.next();
iterator.remove();
fail("iterator.remove should raise UnsupportedOperationException");
内容来源于网络,如有侵权,请联系作者删除!