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

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

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

Collection.size介绍

[英]Returns a count of how many objects this Collection contains.
[中]返回此集合包含多少对象的计数。

代码示例

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

private ClusterException(Collection<? extends Throwable> exceptions) {
 super(
   exceptions.size() + " exceptions were thrown. The first exception is listed as a cause.",
   exceptions.iterator().next());
 ArrayList<Throwable> temp = new ArrayList<>();
 temp.addAll(exceptions);
 this.exceptions = Collections.unmodifiableCollection(temp);
}

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

private static <E> ArrayList<E> toArrayList(Collection<E> c) {
 // Avoid calling ArrayList(Collection), which may call back into toArray.
 ArrayList<E> result = new ArrayList<>(c.size());
 Iterators.addAll(result, c.iterator());
 return result;
}

代码示例来源:origin: hibernate/hibernate-orm

public static boolean[] toBooleanArray(Collection coll) {
  Iterator iter = coll.iterator();
  boolean[] arr = new boolean[coll.size()];
  int i = 0;
  while ( iter.hasNext() ) {
    arr[i++] = (Boolean) iter.next();
  }
  return arr;
}

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

@Test
public void httpHeaderNameCasingIsPreserved() throws Exception {
  final String headerName = "Header1";
  response.addHeader(headerName, "value1");
  Collection<String> responseHeaders = response.getHeaderNames();
  assertNotNull(responseHeaders);
  assertEquals(1, responseHeaders.size());
  assertEquals("HTTP header casing not being preserved", headerName, responseHeaders.iterator().next());
}

代码示例来源:origin: bumptech/glide

@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static List<Key> getAlternateKeys(Collection<String> alternateUrls) {
 List<Key> result = new ArrayList<>(alternateUrls.size());
 for (String alternate : alternateUrls) {
  result.add(new GlideUrl(alternate));
 }
 return result;
}

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

@Test
public void cacheAnnotationOverride() {
  Collection<CacheOperation> ops = getOps(InterfaceCacheConfig.class, "interfaceCacheableOverride");
  assertSame(1, ops.size());
  CacheOperation cacheOperation = ops.iterator().next();
  assertTrue(cacheOperation instanceof CacheableOperation);
}

代码示例来源:origin: thinkaurelius/titan

public static final boolean sizeLargerOrEqualThan(Iterable i, int limit) {
  if (i instanceof Collection) return ((Collection)i).size()>=limit;
  Iterator iter = i.iterator();
  int count=0;
  while (iter.hasNext()) {
    iter.next();
    count++;
    if (count>=limit) return true;
  }
  return false;
}

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

@Test
@SuppressWarnings("rawtypes")
public void convertEmptyStringToCollection() {
  Collection result = conversionService.convert("", Collection.class);
  assertEquals(0, result.size());
}

代码示例来源: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: spring-projects/spring-framework

@Test
public void testBuildCollectionFromMixtureOfReferencesAndValues() throws Exception {
  MixedCollectionBean jumble = (MixedCollectionBean) this.beanFactory.getBean("jumble");
  assertTrue("Expected 3 elements, not " + jumble.getJumble().size(),
      jumble.getJumble().size() == 3);
  List l = (List) jumble.getJumble();
  assertTrue(l.get(0).equals("literal"));
  Integer[] array1 = (Integer[]) l.get(1);
  assertTrue(array1[0].equals(new Integer(2)));
  assertTrue(array1[1].equals(new Integer(4)));
  int[] array2 = (int[]) l.get(2);
  assertTrue(array2[0] == 3);
  assertTrue(array2[1] == 5);
}

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

@SafeVarargs
private static <T> void assertCollectionIs(Collection<T> collection, T... elements) {
  for (T element : elements) {
    assertTrue("Did not find " + element, collection.contains(element));
  }
  assertEquals("There are unexpected extra elements in the collection.",
    elements.length, collection.size());
}

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

/**
 * Test for {@link CustomBean} and an manually endpoint registered
 * with "myCustomEndpointId". The custom endpoint does not provide
 * any factory so it's registered with the default one
 */
public void testCustomConfiguration(ApplicationContext context) {
  JmsListenerContainerTestFactory defaultFactory =
      context.getBean("jmsListenerContainerFactory", JmsListenerContainerTestFactory.class);
  JmsListenerContainerTestFactory customFactory =
      context.getBean("customFactory", JmsListenerContainerTestFactory.class);
  assertEquals(1, defaultFactory.getListenerContainers().size());
  assertEquals(1, customFactory.getListenerContainers().size());
  JmsListenerEndpoint endpoint = defaultFactory.getListenerContainers().get(0).getEndpoint();
  assertEquals("Wrong endpoint type", SimpleJmsListenerEndpoint.class, endpoint.getClass());
  assertEquals("Wrong listener set in custom endpoint", context.getBean("simpleMessageListener"),
      ((SimpleJmsListenerEndpoint) endpoint).getMessageListener());
  JmsListenerEndpointRegistry customRegistry =
      context.getBean("customRegistry", JmsListenerEndpointRegistry.class);
  assertEquals("Wrong number of containers in the registry", 2,
      customRegistry.getListenerContainerIds().size());
  assertEquals("Wrong number of containers in the registry", 2,
      customRegistry.getListenerContainers().size());
  assertNotNull("Container with custom id on the annotation should be found",
      customRegistry.getListenerContainer("listenerId"));
  assertNotNull("Container created with custom id should be found",
      customRegistry.getListenerContainer("myCustomEndpointId"));
}

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

@Test
public void testWhenTargetTypeIsExactlyTheCollectionInterfaceUsesFallbackCollectionType() throws Exception {
  CustomCollectionEditor editor = new CustomCollectionEditor(Collection.class);
  editor.setValue("0, 1, 2");
  Collection<?> value = (Collection<?>) editor.getValue();
  assertNotNull(value);
  assertEquals("There must be 1 element in the converted collection", 1, value.size());
  assertEquals("0, 1, 2", value.iterator().next());
}

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

private String style(Collection<?> value) {
  StringBuilder result = new StringBuilder(value.size() * 8 + 16);
  result.append(getCollectionTypeString(value)).append('[');
  for (Iterator<?> i = value.iterator(); i.hasNext();) {
    result.append(style(i.next()));
    if (i.hasNext()) {
      result.append(',').append(' ');
    }
  }
  if (value.isEmpty()) {
    result.append(EMPTY);
  }
  result.append("]");
  return result.toString();
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Removes the embedded console notes in the given log lines.
 *
 * @since 1.350
 */
public static List<String> removeNotes(Collection<String> logLines) {
  List<String> r = new ArrayList<String>(logLines.size());
  for (String l : logLines)
    r.add(removeNotes(l));
  return r;
}

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

@Test
public void testMultipleCachingSource() {
  Collection<CacheOperation> ops = getOps("multipleCaching");
  assertEquals(2, ops.size());
  Iterator<CacheOperation> it = ops.iterator();
  CacheOperation next = it.next();
  assertTrue(next instanceof CacheableOperation);
  assertTrue(next.getCacheNames().contains("test"));
  assertEquals("#a", next.getKey());
  next = it.next();
  assertTrue(next instanceof CacheableOperation);
  assertTrue(next.getCacheNames().contains("test"));
  assertEquals("#b", next.getKey());
}

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

/**
 * Collect any {@link AsyncConfigurer} beans through autowiring.
 */
@Autowired(required = false)
void setConfigurers(Collection<AsyncConfigurer> configurers) {
  if (CollectionUtils.isEmpty(configurers)) {
    return;
  }
  if (configurers.size() > 1) {
    throw new IllegalStateException("Only one AsyncConfigurer may exist");
  }
  AsyncConfigurer configurer = configurers.iterator().next();
  this.executor = configurer::getAsyncExecutor;
  this.exceptionHandler = configurer::getAsyncUncaughtExceptionHandler;
}

代码示例来源: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: spring-projects/spring-framework

@Test
public void combine() {
  HeadersRequestCondition condition1 = new HeadersRequestCondition("foo=bar");
  HeadersRequestCondition condition2 = new HeadersRequestCondition("foo=baz");
  HeadersRequestCondition result = condition1.combine(condition2);
  Collection<HeaderExpression> conditions = result.getContent();
  assertEquals(2, conditions.size());
}

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

static void checkEmpty(Collection<?> collection) {
  assertTrue(collection.isEmpty());
  assertEquals(0, collection.size());
  assertFalse(collection.iterator().hasNext());
  assertThat(collection.toArray()).isEmpty();
  assertThat(collection.toArray(new Object[0])).isEmpty();
  if (collection instanceof Set) {
   new EqualsTester()
     .addEqualityGroup(ImmutableSet.of(), collection)
     .addEqualityGroup(ImmutableSet.of(""))
     .testEquals();
  } else if (collection instanceof List) {
   new EqualsTester()
     .addEqualityGroup(ImmutableList.of(), collection)
     .addEqualityGroup(ImmutableList.of(""))
     .testEquals();
  }
 }
}

相关文章