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

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

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

Collection.retainAll介绍

[英]Removes all objects from this Collection that are not also found in the Collection passed (optional). After this method returns this Collectionwill only contain elements that also can be found in the Collectionpassed to this method.
[中]从该集合中删除在传递的集合中找不到的所有对象(可选)。此方法返回后,此集合将仅包含在传递给此方法的集合中也可以找到的元素。

代码示例

代码示例来源:origin: hankcs/HanLP

@Override
public boolean retainAll(Collection<?> c)
{
  return termFrequencyMap.values().retainAll(c);
}

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

@Override
public boolean retainAll(Collection<?> c) {
 synchronized (mutex) {
  return delegate().retainAll(c);
 }
}

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

@Override
public boolean retainAll(Collection<?> c) {
 synchronized (mutex) {
  return delegate().retainAll(c);
 }
}

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

@Override
 public boolean retainAll(Collection<?> c) {
  return delegate.values().retainAll(c);
 }
};

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

public void testValuesRetainAllNullFromEmpty() {
 final Map<K, V> map;
 try {
  map = makeEmptyMap();
 } catch (UnsupportedOperationException e) {
  return;
 }
 Collection<V> values = map.values();
 if (supportsRemove) {
  try {
   values.retainAll(null);
   // Returning successfully is not ideal, but tolerated.
  } catch (NullPointerException expected) {
  }
 } else {
  try {
   values.retainAll(null);
   // We have to tolerate a successful return (Sun bug 4802647)
  } catch (UnsupportedOperationException | NullPointerException e) {
   // Expected.
  }
 }
 assertInvariants(map);
}

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

/**
 * Removes, from an iterable, every element that does not belong to the provided collection.
 *
 * <p>This method calls {@link Collection#retainAll} if {@code iterable} is a collection, and
 * {@link Iterators#retainAll} otherwise.
 *
 * @param removeFrom the iterable to (potentially) remove elements from
 * @param elementsToRetain the elements to retain
 * @return {@code true} if any element was removed from {@code iterable}
 */
@CanIgnoreReturnValue
public static boolean retainAll(Iterable<?> removeFrom, Collection<?> elementsToRetain) {
 return (removeFrom instanceof Collection)
   ? ((Collection<?>) removeFrom).retainAll(checkNotNull(elementsToRetain))
   : Iterators.retainAll(removeFrom.iterator(), elementsToRetain);
}

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

@CanIgnoreReturnValue
@Override
public boolean retainAll(Collection<?> collection) {
 return delegate().retainAll(collection);
}

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

private void expectThrows(Target target) {
 try {
  collection.retainAll(target.toRetain);
  String message = Platform.format("retainAll(%s) should throw", target);
  fail(message);
 } catch (UnsupportedOperationException expected) {
 }
}

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

private void expectReturnsFalseOrThrows(Target target) {
  String message = Platform.format("retainAll(%s) should return false or throw", target);
  try {
   assertFalse(message, collection.retainAll(target.toRetain));
  } catch (UnsupportedOperationException tolerated) {
  }
 }
}

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

@CollectionFeature.Require(SUPPORTS_REMOVE)
@CollectionSize.Require(ZERO)
public void testRetainAll_nullCollectionReferenceEmptySubject() {
 try {
  collection.retainAll(null);
  // Returning successfully is not ideal, but tolerated.
 } catch (NullPointerException tolerated) {
 }
}

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

private void expectReturnsFalse(Target target) {
 String message = Platform.format("retainAll(%s) should return false", target);
 assertFalse(message, collection.retainAll(target.toRetain));
}

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

private void expectReturnsTrue(Target target) {
 String message = Platform.format("retainAll(%s) should return true", target);
 assertTrue(message, collection.retainAll(target.toRetain));
}

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

@CanIgnoreReturnValue
@Override
public boolean retainAll(Collection<?> collection) {
 return delegate().retainAll(collection);
}

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

@CollectionFeature.Require(SUPPORTS_REMOVE)
@CollectionSize.Require(absent = ZERO)
public void testRetainAll_nullCollectionReferenceNonEmptySubject() {
 try {
  collection.retainAll(null);
  fail("retainAll(null) should throw NullPointerException");
 } catch (NullPointerException expected) {
 }
}

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

private Collection<String> getIntersectionDataSources() {
  Collection<String> result = new HashSet<>();
  for (RoutingResult each : routingResults) {
    if (result.isEmpty()) {
      result.addAll(each.getTableUnits().getDataSourceNames());
    }
    result.retainAll(each.getTableUnits().getDataSourceNames());
  }
  return result;
}

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

public void testValues_empty_remove() {
 for (LoadingCache<Object, Object> cache : caches()) {
  Collection<Object> values = cache.asMap().values();
  assertFalse(values.remove(null));
  assertFalse(values.remove(6));
  assertFalse(values.remove(-6));
  assertFalse(values.removeAll(asList(null, 0, 15, 1500)));
  assertFalse(values.retainAll(asList(null, 0, 15, 1500)));
  checkEmpty(values);
  checkEmpty(cache);
 }
}

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

public void testValues_remove() {
 for (LoadingCache<Object, Object> cache : caches()) {
  cache.getUnchecked(1);
  cache.getUnchecked(2);
  Collection<Object> values = cache.asMap().keySet();
  // We don't know whether these are still in the cache, so we can't assert on the return
  // values of these removes, but the cache should be empty after the removes, regardless.
  values.remove(1);
  values.remove(2);
  assertFalse(values.remove(null));
  assertFalse(values.remove(6));
  assertFalse(values.remove(-6));
  assertFalse(values.removeAll(asList(null, 0, 15, 1500)));
  assertFalse(values.retainAll(asList(null, 0, 15, 1500)));
  checkEmpty(values);
  checkEmpty(cache);
 }
}

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

@CollectionSize.Require(absent = ZERO)
@MapFeature.Require(SUPPORTS_REMOVE)
public void testRetainAllPropagatesToMultimap() {
 multimap().entries().retainAll(Collections.singleton(Helpers.mapEntry(k0(), v0())));
 assertEquals(getSubjectGenerator().create(Helpers.mapEntry(k0(), v0())), multimap());
 assertEquals(1, multimap().size());
 assertTrue(multimap().containsEntry(k0(), v0()));
}

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

@CollectionFeature.Require(SUPPORTS_REMOVE)
@CollectionSize.Require(absent = {ZERO, ONE})
public void testRetainAll_duplicatesKept() {
 E[] array = createSamplesArray();
 array[1] = e0();
 collection = getSubjectGenerator().create(array);
 assertFalse(
   "containsDuplicates.retainAll(superset) should return false",
   collection.retainAll(MinimalCollection.of(createSamplesArray())));
 expectContents(array);
}

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

@SuppressWarnings("unchecked")
@CollectionFeature.Require(SUPPORTS_REMOVE)
@CollectionSize.Require(SEVERAL)
public void testRetainAll_duplicatesRemoved() {
 E[] array = createSamplesArray();
 array[1] = e0();
 collection = getSubjectGenerator().create(array);
 assertTrue(
   "containsDuplicates.retainAll(subset) should return true",
   collection.retainAll(MinimalCollection.of(e2())));
 expectContents(e2());
}

相关文章