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

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

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

Collection.contains介绍

[英]Tests whether this Collection contains the specified object. Returns true if and only if at least one element elem in this Collection meets following requirement: (object==null ? elem==null : object.equals(elem)).
[中]测试此集合是否包含指定的对象。当且仅当此集合中至少有一个元素elem满足以下要求时返回true:(object==null?elem==null:object.equals(elem))。

代码示例

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

@Override
public Broadcaster addBroadcasterListener(BroadcasterListener b) {
  if (!sharedListeners && !broadcasterListeners.contains(b)) {
    broadcasterListeners.add(b);
  }
  return this;
}

代码示例来源:origin: apache/ignite

@Override public boolean apply(T t) {
    return (!(c == null || c.isEmpty())) && c.contains(t);
  }
};

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

@Override
public RoutingResult route() {
  Collection<RoutingResult> result = new ArrayList<>(logicTables.size());
  Collection<String> bindingTableNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
  for (String each : logicTables) {
    Optional<TableRule> tableRule = shardingRule.findTableRule(each);
    if (tableRule.isPresent()) {
      if (!bindingTableNames.contains(each)) {
        result.add(new StandardRoutingEngine(shardingRule, tableRule.get().getLogicTable(), shardingConditions).route());
  if (result.isEmpty()) {
    throw new ShardingException("Cannot find table rule and default data source with logic tables: '%s'", logicTables);
  if (1 == result.size()) {
    return result.iterator().next();

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

public void testValuesRemoveAll() {
 final Map<K, V> map;
 try {
  map = makePopulatedMap();
 } catch (UnsupportedOperationException e) {
  return;
 }
 Collection<V> valueCollection = map.values();
 Set<V> valuesToRemove = singleton(valueCollection.iterator().next());
 if (supportsRemove) {
  valueCollection.removeAll(valuesToRemove);
  for (V value : valuesToRemove) {
   assertFalse(valueCollection.contains(value));
  }
  for (V value : valueCollection) {
   assertFalse(valuesToRemove.contains(value));
  }
 } else {
  try {
   valueCollection.removeAll(valuesToRemove);
   fail("Expected UnsupportedOperationException.");
  } catch (UnsupportedOperationException expected) {
  }
 }
 assertInvariants(map);
}

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

assertEquals(map.size(), keySet.size());
assertEquals(keySet.size() == 0, keySet.isEmpty());
assertEquals(!keySet.isEmpty(), keySet.iterator().hasNext());
 assertTrue(map.containsKey(key));
 assertTrue(map.containsValue(value));
 assertTrue(valueCollection.contains(value));
 assertTrue(valueCollection.containsAll(Collections.singleton(value)));
 assertTrue(entrySet.contains(mapEntry(key, value)));
assertEquals(map.size(), valueCollection.size());
assertEquals(valueCollection.size() == 0, valueCollection.isEmpty());
assertEquals(!valueCollection.isEmpty(), valueCollection.iterator().hasNext());
for (V value : valueCollection) {
 assertTrue(map.containsValue(value));
assertEquals(!entrySet.isEmpty(), entrySet.iterator().hasNext());
assertEntrySetNotContainsString(entrySet);

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

private List<ShardingValue> getShardingValuesFromShardingConditions(final Collection<String> shardingColumns, final ShardingCondition shardingCondition) {
  List<ShardingValue> result = new ArrayList<>(shardingColumns.size());
  for (ShardingValue each : shardingCondition.getShardingValues()) {
    Optional<BindingTableRule> bindingTableRule = shardingRule.findBindingTableRule(logicTableName);
    if ((logicTableName.equals(each.getLogicTableName()) || bindingTableRule.isPresent() && bindingTableRule.get().hasLogicTable(logicTableName)) 
        && shardingColumns.contains(each.getColumnName())) {
      result.add(each);
    }
  }
  return result;
}

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

public void testIterator() {
  setUpTest();
  one.add("1");
  two.add("2");
  c.addComposited(one);
  c.addComposited(two);
  Iterator i = c.iterator();
  Object next = i.next();
  assertTrue(c.contains(next));
  assertTrue(one.contains(next));
  next = i.next();
  i.remove();
  assertTrue(!c.contains(next));
  assertTrue(!two.contains(next));
}

代码示例来源:origin: apache/hive

private static Collection<String> catalogsToCache(RawStore rs) throws MetaException {
 Collection<String> confValue =
   MetastoreConf.getStringCollection(rs.getConf(), ConfVars.CATALOGS_TO_CACHE);
 if (confValue == null || confValue.isEmpty() ||
   (confValue.size() == 1 && confValue.contains(""))) {
  return rs.getCatalogs();
 } else {
  return confValue;
 }
}

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

public void testPermutationSetEmpty() {
 Collection<List<Integer>> permutationSet =
   Collections2.permutations(Collections.<Integer>emptyList());
 assertEquals(1, permutationSet.size());
 assertTrue(permutationSet.contains(Collections.<Integer>emptyList()));
 Iterator<List<Integer>> permutations = permutationSet.iterator();
 assertNextPermutation(Collections.<Integer>emptyList(), permutations);
 assertNoMorePermutations(permutations);
}

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

public void testTransformedCollection() {
  Collection coll = TransformedCollection.decorate(new ArrayList(), STRING_TO_INTEGER_TRANSFORMER);
  assertEquals(0, coll.size());
  Object[] els = getFullElements();
  for (int i = 0; i < els.length; i++) {
    coll.add(els[i]);
    assertEquals(i + 1, coll.size());
    assertEquals(true, coll.contains(new Integer((String) els[i])));
    assertEquals(false, coll.contains(els[i]));
  }
  
  assertEquals(true, coll.remove(new Integer((String) els[0])));
}

代码示例来源:origin: MovingBlocks/Terasology

@Test
public void testCollectionMethods() {
  Collection<Integer> buffer = CircularBuffer.create(4);
  buffer.addAll(ImmutableList.of(1, 2, 3, 4, 5, 6));
  buffer.add(4);
  assertTrue(buffer.contains(5));
  assertTrue(buffer.containsAll(ImmutableList.of(5, 4)));
}

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

static <K, V> boolean retainAllKeys(
  Map<K, V> map, Predicate<? super Entry<K, V>> entryPredicate, Collection<?> keyCollection) {
 Iterator<Entry<K, V>> entryItr = map.entrySet().iterator();
 boolean result = false;
 while (entryItr.hasNext()) {
  Entry<K, V> entry = entryItr.next();
  if (entryPredicate.apply(entry) && !keyCollection.contains(entry.getKey())) {
   entryItr.remove();
   result = true;
  }
 }
 return result;
}

代码示例来源:origin: apache/storm

if (freeSlots.isEmpty()) {
  throw new IllegalStateException("Trying to assign to a full node " + nodeId);
if (executors.size() == 0) {
  LOG.warn("Trying to assign nothing from " + td.getId() + " to " + nodeId + " (Ignored)");
  target = getFreeSlots().iterator().next();
if (!freeSlots.contains(target)) {
  throw new IllegalStateException(
    "Trying to assign already used slot " + target.getPort() + " on node " + nodeId);

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

public void testValuesRetainAll() {
 final Map<K, V> map;
 try {
  map = makePopulatedMap();
 } catch (UnsupportedOperationException e) {
  return;
 }
 Collection<V> valueCollection = map.values();
 Set<V> valuesToRetain = singleton(valueCollection.iterator().next());
 if (supportsRemove) {
  valueCollection.retainAll(valuesToRetain);
  for (V value : valuesToRetain) {
   assertTrue(valueCollection.contains(value));
  }
  for (V value : valueCollection) {
   assertTrue(valuesToRetain.contains(value));
  }
 } else {
  try {
   valueCollection.retainAll(valuesToRetain);
   fail("Expected UnsupportedOperationException.");
  } catch (UnsupportedOperationException expected) {
  }
 }
 assertInvariants(map);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected void initServletContext(ServletContext servletContext) {
  Collection<ViewResolver> matchingBeans =
      BeanFactoryUtils.beansOfTypeIncludingAncestors(obtainApplicationContext(), ViewResolver.class).values();
  if (this.viewResolvers == null) {
    this.viewResolvers = new ArrayList<>(matchingBeans.size());
    for (ViewResolver viewResolver : matchingBeans) {
      if (this != viewResolver) {
        this.viewResolvers.add(viewResolver);
      }
    }
  }
  else {
    for (int i = 0; i < this.viewResolvers.size(); i++) {
      ViewResolver vr = this.viewResolvers.get(i);
      if (matchingBeans.contains(vr)) {
        continue;
      }
      String name = vr.getClass().getName() + i;
      obtainApplicationContext().getAutowireCapableBeanFactory().initializeBean(vr, name);
    }
  }
  AnnotationAwareOrderComparator.sort(this.viewResolvers);
  this.cnmFactoryBean.setServletContext(servletContext);
}

代码示例来源:origin: bytedeco/javacpp

public void addClass(Class c) {
  if (!classes.contains(c)) {
    classes.add(c);
  }
}

代码示例来源:origin: apache/ignite

/**
 * @throws Exception If failed.
 */
@Test
public void testCallAsyncMultiple() throws Exception {
  Collection<ClosureTestCallable> jobs = F.asList(new ClosureTestCallable(), new ClosureTestCallable());
  IgniteFuture<Collection<Integer>> fut = callAsync(0, jobs, null);
  Collection<Integer> results = fut.get();
  assert !results.isEmpty() : "Collection of results is empty.";
  assert results.size() == jobs.size() :
    "Collection of results must be of size: " + jobs.size() + ".";
  for (int i = 1; i <= jobs.size(); i++)
    assert results.contains(i) : "Collection of results does not contain value: " + i;
}

代码示例来源:origin: SonarSource/sonarqube

public static boolean isFieldNeeded(String field, @Nullable Collection<String> fields) {
  return fields == null || fields.isEmpty() || fields.contains(field);
 }
}

代码示例来源:origin: apache/ignite

/**
 * @param set Set.
 * @param size Expected size.
 */
private void assertSetContent(IgniteSet<Integer> set, int size) {
  Collection<Integer> data = new HashSet<>(size);
  for (Integer val : set)
    assertTrue(data.add(val));
  assertEquals(size, data.size());
  for (int val = 0; val < size; val++)
    assertTrue(data.contains(val));
}

相关文章