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

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

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

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

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

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

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

  1. public static boolean[] toBooleanArray(Collection coll) {
  2. Iterator iter = coll.iterator();
  3. boolean[] arr = new boolean[coll.size()];
  4. int i = 0;
  5. while ( iter.hasNext() ) {
  6. arr[i++] = (Boolean) iter.next();
  7. }
  8. return arr;
  9. }

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

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

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

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

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

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

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

  1. public static final boolean sizeLargerOrEqualThan(Iterable i, int limit) {
  2. if (i instanceof Collection) return ((Collection)i).size()>=limit;
  3. Iterator iter = i.iterator();
  4. int count=0;
  5. while (iter.hasNext()) {
  6. iter.next();
  7. count++;
  8. if (count>=limit) return true;
  9. }
  10. return false;
  11. }

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

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

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

  1. @CollectionFeature.Require(SUPPORTS_REMOVE)
  2. public void testClear() {
  3. collection.clear();
  4. assertTrue("After clear(), a collection should be empty.", collection.isEmpty());
  5. assertEquals(0, collection.size());
  6. assertFalse(collection.iterator().hasNext());
  7. }

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

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

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

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

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

  1. /**
  2. * Test for {@link CustomBean} and an manually endpoint registered
  3. * with "myCustomEndpointId". The custom endpoint does not provide
  4. * any factory so it's registered with the default one
  5. */
  6. public void testCustomConfiguration(ApplicationContext context) {
  7. JmsListenerContainerTestFactory defaultFactory =
  8. context.getBean("jmsListenerContainerFactory", JmsListenerContainerTestFactory.class);
  9. JmsListenerContainerTestFactory customFactory =
  10. context.getBean("customFactory", JmsListenerContainerTestFactory.class);
  11. assertEquals(1, defaultFactory.getListenerContainers().size());
  12. assertEquals(1, customFactory.getListenerContainers().size());
  13. JmsListenerEndpoint endpoint = defaultFactory.getListenerContainers().get(0).getEndpoint();
  14. assertEquals("Wrong endpoint type", SimpleJmsListenerEndpoint.class, endpoint.getClass());
  15. assertEquals("Wrong listener set in custom endpoint", context.getBean("simpleMessageListener"),
  16. ((SimpleJmsListenerEndpoint) endpoint).getMessageListener());
  17. JmsListenerEndpointRegistry customRegistry =
  18. context.getBean("customRegistry", JmsListenerEndpointRegistry.class);
  19. assertEquals("Wrong number of containers in the registry", 2,
  20. customRegistry.getListenerContainerIds().size());
  21. assertEquals("Wrong number of containers in the registry", 2,
  22. customRegistry.getListenerContainers().size());
  23. assertNotNull("Container with custom id on the annotation should be found",
  24. customRegistry.getListenerContainer("listenerId"));
  25. assertNotNull("Container created with custom id should be found",
  26. customRegistry.getListenerContainer("myCustomEndpointId"));
  27. }

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

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

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

  1. private String style(Collection<?> value) {
  2. StringBuilder result = new StringBuilder(value.size() * 8 + 16);
  3. result.append(getCollectionTypeString(value)).append('[');
  4. for (Iterator<?> i = value.iterator(); i.hasNext();) {
  5. result.append(style(i.next()));
  6. if (i.hasNext()) {
  7. result.append(',').append(' ');
  8. }
  9. }
  10. if (value.isEmpty()) {
  11. result.append(EMPTY);
  12. }
  13. result.append("]");
  14. return result.toString();
  15. }

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

  1. /**
  2. * Removes the embedded console notes in the given log lines.
  3. *
  4. * @since 1.350
  5. */
  6. public static List<String> removeNotes(Collection<String> logLines) {
  7. List<String> r = new ArrayList<String>(logLines.size());
  8. for (String l : logLines)
  9. r.add(removeNotes(l));
  10. return r;
  11. }

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

  1. @Test
  2. public void testMultipleCachingSource() {
  3. Collection<CacheOperation> ops = getOps("multipleCaching");
  4. assertEquals(2, ops.size());
  5. Iterator<CacheOperation> it = ops.iterator();
  6. CacheOperation next = it.next();
  7. assertTrue(next instanceof CacheableOperation);
  8. assertTrue(next.getCacheNames().contains("test"));
  9. assertEquals("#a", next.getKey());
  10. next = it.next();
  11. assertTrue(next instanceof CacheableOperation);
  12. assertTrue(next.getCacheNames().contains("test"));
  13. assertEquals("#b", next.getKey());
  14. }

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

  1. /**
  2. * Collect any {@link AsyncConfigurer} beans through autowiring.
  3. */
  4. @Autowired(required = false)
  5. void setConfigurers(Collection<AsyncConfigurer> configurers) {
  6. if (CollectionUtils.isEmpty(configurers)) {
  7. return;
  8. }
  9. if (configurers.size() > 1) {
  10. throw new IllegalStateException("Only one AsyncConfigurer may exist");
  11. }
  12. AsyncConfigurer configurer = configurers.iterator().next();
  13. this.executor = configurer::getAsyncExecutor;
  14. this.exceptionHandler = configurer::getAsyncUncaughtExceptionHandler;
  15. }

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

  1. boolean removeEntriesIf(Predicate<? super Entry<K, Collection<V>>> predicate) {
  2. Iterator<Entry<K, Collection<V>>> entryIterator = unfiltered.asMap().entrySet().iterator();
  3. boolean changed = false;
  4. while (entryIterator.hasNext()) {
  5. Entry<K, Collection<V>> entry = entryIterator.next();
  6. K key = entry.getKey();
  7. Collection<V> collection = filterCollection(entry.getValue(), new ValuePredicate(key));
  8. if (!collection.isEmpty() && predicate.apply(Maps.immutableEntry(key, collection))) {
  9. if (collection.size() == entry.getValue().size()) {
  10. entryIterator.remove();
  11. } else {
  12. collection.clear();
  13. }
  14. changed = true;
  15. }
  16. }
  17. return changed;
  18. }

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

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

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

  1. static void checkEmpty(Collection<?> collection) {
  2. assertTrue(collection.isEmpty());
  3. assertEquals(0, collection.size());
  4. assertFalse(collection.iterator().hasNext());
  5. assertThat(collection.toArray()).isEmpty();
  6. assertThat(collection.toArray(new Object[0])).isEmpty();
  7. if (collection instanceof Set) {
  8. new EqualsTester()
  9. .addEqualityGroup(ImmutableSet.of(), collection)
  10. .addEqualityGroup(ImmutableSet.of(""))
  11. .testEquals();
  12. } else if (collection instanceof List) {
  13. new EqualsTester()
  14. .addEqualityGroup(ImmutableList.of(), collection)
  15. .addEqualityGroup(ImmutableList.of(""))
  16. .testEquals();
  17. }
  18. }
  19. }

相关文章