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

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

本文整理了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

  1. @Override
  2. public boolean removeAll(Collection<?> collection) {
  3. Iterator<Entry<K, V>> entryItr = unfiltered.entrySet().iterator();
  4. boolean result = false;
  5. while (entryItr.hasNext()) {
  6. Entry<K, V> entry = entryItr.next();
  7. if (predicate.apply(entry) && collection.contains(entry.getValue())) {
  8. entryItr.remove();
  9. result = true;
  10. }
  11. }
  12. return result;
  13. }

代码示例来源:origin: Atmosphere/atmosphere

  1. @Override
  2. public Broadcaster addBroadcasterListener(BroadcasterListener b) {
  3. if (!sharedListeners && !broadcasterListeners.contains(b)) {
  4. broadcasterListeners.add(b);
  5. }
  6. return this;
  7. }

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

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

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

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

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

  1. public void testValuesRemoveAll() {
  2. final Map<K, V> map;
  3. try {
  4. map = makePopulatedMap();
  5. } catch (UnsupportedOperationException e) {
  6. return;
  7. }
  8. Collection<V> valueCollection = map.values();
  9. Set<V> valuesToRemove = singleton(valueCollection.iterator().next());
  10. if (supportsRemove) {
  11. valueCollection.removeAll(valuesToRemove);
  12. for (V value : valuesToRemove) {
  13. assertFalse(valueCollection.contains(value));
  14. }
  15. for (V value : valueCollection) {
  16. assertFalse(valuesToRemove.contains(value));
  17. }
  18. } else {
  19. try {
  20. valueCollection.removeAll(valuesToRemove);
  21. fail("Expected UnsupportedOperationException.");
  22. } catch (UnsupportedOperationException expected) {
  23. }
  24. }
  25. assertInvariants(map);
  26. }

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

  1. assertEquals(map.size(), keySet.size());
  2. assertEquals(keySet.size() == 0, keySet.isEmpty());
  3. assertEquals(!keySet.isEmpty(), keySet.iterator().hasNext());
  4. assertTrue(map.containsKey(key));
  5. assertTrue(map.containsValue(value));
  6. assertTrue(valueCollection.contains(value));
  7. assertTrue(valueCollection.containsAll(Collections.singleton(value)));
  8. assertTrue(entrySet.contains(mapEntry(key, value)));
  9. assertEquals(map.size(), valueCollection.size());
  10. assertEquals(valueCollection.size() == 0, valueCollection.isEmpty());
  11. assertEquals(!valueCollection.isEmpty(), valueCollection.iterator().hasNext());
  12. for (V value : valueCollection) {
  13. assertTrue(map.containsValue(value));
  14. assertEquals(!entrySet.isEmpty(), entrySet.iterator().hasNext());
  15. assertEntrySetNotContainsString(entrySet);

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

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

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

  1. public void testIterator() {
  2. setUpTest();
  3. one.add("1");
  4. two.add("2");
  5. c.addComposited(one);
  6. c.addComposited(two);
  7. Iterator i = c.iterator();
  8. Object next = i.next();
  9. assertTrue(c.contains(next));
  10. assertTrue(one.contains(next));
  11. next = i.next();
  12. i.remove();
  13. assertTrue(!c.contains(next));
  14. assertTrue(!two.contains(next));
  15. }

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

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

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

  1. public void testPermutationSetEmpty() {
  2. Collection<List<Integer>> permutationSet =
  3. Collections2.permutations(Collections.<Integer>emptyList());
  4. assertEquals(1, permutationSet.size());
  5. assertTrue(permutationSet.contains(Collections.<Integer>emptyList()));
  6. Iterator<List<Integer>> permutations = permutationSet.iterator();
  7. assertNextPermutation(Collections.<Integer>emptyList(), permutations);
  8. assertNoMorePermutations(permutations);
  9. }

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

  1. public void testTransformedCollection() {
  2. Collection coll = TransformedCollection.decorate(new ArrayList(), STRING_TO_INTEGER_TRANSFORMER);
  3. assertEquals(0, coll.size());
  4. Object[] els = getFullElements();
  5. for (int i = 0; i < els.length; i++) {
  6. coll.add(els[i]);
  7. assertEquals(i + 1, coll.size());
  8. assertEquals(true, coll.contains(new Integer((String) els[i])));
  9. assertEquals(false, coll.contains(els[i]));
  10. }
  11. assertEquals(true, coll.remove(new Integer((String) els[0])));
  12. }

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

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

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

  1. static <K, V> boolean retainAllKeys(
  2. Map<K, V> map, Predicate<? super Entry<K, V>> entryPredicate, Collection<?> keyCollection) {
  3. Iterator<Entry<K, V>> entryItr = map.entrySet().iterator();
  4. boolean result = false;
  5. while (entryItr.hasNext()) {
  6. Entry<K, V> entry = entryItr.next();
  7. if (entryPredicate.apply(entry) && !keyCollection.contains(entry.getKey())) {
  8. entryItr.remove();
  9. result = true;
  10. }
  11. }
  12. return result;
  13. }

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

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

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

  1. public void testValuesRetainAll() {
  2. final Map<K, V> map;
  3. try {
  4. map = makePopulatedMap();
  5. } catch (UnsupportedOperationException e) {
  6. return;
  7. }
  8. Collection<V> valueCollection = map.values();
  9. Set<V> valuesToRetain = singleton(valueCollection.iterator().next());
  10. if (supportsRemove) {
  11. valueCollection.retainAll(valuesToRetain);
  12. for (V value : valuesToRetain) {
  13. assertTrue(valueCollection.contains(value));
  14. }
  15. for (V value : valueCollection) {
  16. assertTrue(valuesToRetain.contains(value));
  17. }
  18. } else {
  19. try {
  20. valueCollection.retainAll(valuesToRetain);
  21. fail("Expected UnsupportedOperationException.");
  22. } catch (UnsupportedOperationException expected) {
  23. }
  24. }
  25. assertInvariants(map);
  26. }

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

  1. @Override
  2. protected void initServletContext(ServletContext servletContext) {
  3. Collection<ViewResolver> matchingBeans =
  4. BeanFactoryUtils.beansOfTypeIncludingAncestors(obtainApplicationContext(), ViewResolver.class).values();
  5. if (this.viewResolvers == null) {
  6. this.viewResolvers = new ArrayList<>(matchingBeans.size());
  7. for (ViewResolver viewResolver : matchingBeans) {
  8. if (this != viewResolver) {
  9. this.viewResolvers.add(viewResolver);
  10. }
  11. }
  12. }
  13. else {
  14. for (int i = 0; i < this.viewResolvers.size(); i++) {
  15. ViewResolver vr = this.viewResolvers.get(i);
  16. if (matchingBeans.contains(vr)) {
  17. continue;
  18. }
  19. String name = vr.getClass().getName() + i;
  20. obtainApplicationContext().getAutowireCapableBeanFactory().initializeBean(vr, name);
  21. }
  22. }
  23. AnnotationAwareOrderComparator.sort(this.viewResolvers);
  24. this.cnmFactoryBean.setServletContext(servletContext);
  25. }

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

  1. public void addClass(Class c) {
  2. if (!classes.contains(c)) {
  3. classes.add(c);
  4. }
  5. }

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

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

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

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

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

  1. /**
  2. * @param set Set.
  3. * @param size Expected size.
  4. */
  5. private void assertSetContent(IgniteSet<Integer> set, int size) {
  6. Collection<Integer> data = new HashSet<>(size);
  7. for (Integer val : set)
  8. assertTrue(data.add(val));
  9. assertEquals(size, data.size());
  10. for (int val = 0; val < size; val++)
  11. assertTrue(data.contains(val));
  12. }

相关文章