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

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

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

Collection.isEmpty介绍

[英]Returns if this Collection contains no elements.
[中]如果此集合不包含元素,则返回。

代码示例

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

  1. private static boolean isEmpty(Iterable<?> iterable) {
  2. return iterable instanceof Collection
  3. ? ((Collection<?>) iterable).isEmpty()
  4. : !iterable.iterator().hasNext();
  5. }

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

  1. /** Used during deserialization only. */
  2. final void setMap(Map<K, Collection<V>> map) {
  3. this.map = map;
  4. totalSize = 0;
  5. for (Collection<V> values : map.values()) {
  6. checkArgument(!values.isEmpty());
  7. totalSize += values.size();
  8. }
  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: redisson/redisson

  1. @Override
  2. public RFuture<Boolean> addAllAsync(Collection<? extends String> c) {
  3. if (c.isEmpty()) {
  4. return RedissonPromise.newSucceededFuture(false);
  5. }
  6. List<Object> params = new ArrayList<Object>(2*c.size());
  7. params.add(getName());
  8. for (Object param : c) {
  9. params.add(0);
  10. params.add(param);
  11. }
  12. return commandExecutor.writeAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZADD_BOOL_RAW, params.toArray());
  13. }

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

  1. RoutingResult result = new RoutingResult();
  2. if (shardingRule.isAllBroadcastTables(logicTables)) {
  3. List<RoutingTable> routingTables = new ArrayList<>(logicTables.size());
  4. for (String each : logicTables) {
  5. routingTables.add(new RoutingTable(each, each));
  6. result.getTableUnits().getTableUnits().add(tableUnit);
  7. } else if (logicTables.isEmpty()) {
  8. result.getTableUnits().getTableUnits().add(new TableUnit(shardingRule.getShardingDataSourceNames().getRandomDataSourceName()));
  9. } else if (1 == logicTables.size()) {
  10. String logicTableName = logicTables.iterator().next();
  11. DataNode dataNode = shardingRule.getDataNode(logicTableName);
  12. TableUnit tableUnit = new TableUnit(dataNode.getDataSourceName());
  13. tableUnit.getRoutingTables().add(new RoutingTable(logicTableName, dataNode.getTableName()));
  14. result.getTableUnits().getTableUnits().add(tableUnit);
  15. } else {
  16. List<RoutingTable> routingTables = new ArrayList<>(logicTables.size());
  17. Set<String> availableDatasourceNames = null;
  18. boolean first = true;
  19. TableRule tableRule = shardingRule.getTableRule(each);
  20. DataNode dataNode = tableRule.getActualDataNodes().get(0);
  21. routingTables.add(new RoutingTable(each, dataNode.getTableName()));
  22. Set<String> currentDataSourceNames = new HashSet<>(tableRule.getActualDatasourceNames().size());
  23. for (DataNode eachDataNode : tableRule.getActualDataNodes()) {

代码示例来源:origin: stanfordnlp/CoreNLP

  1. /** Returns the (first) root of this SemanticGraph. */
  2. public IndexedWord getFirstRoot() {
  3. if (roots.isEmpty())
  4. throw new RuntimeException("No roots in graph:\n" + this
  5. + "\nFind where this graph was created and make sure you're adding roots.");
  6. return roots.iterator().next();
  7. }

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

  1. public boolean removeAll(Collection coll) {
  2. if (parent.isEmpty() || coll.isEmpty()) {
  3. return false;
  4. }
  5. boolean modified = false;
  6. Iterator it = iterator();
  7. while (it.hasNext()) {
  8. if (coll.contains(it.next())) {
  9. it.remove();
  10. modified = true;
  11. }
  12. }
  13. return modified;
  14. }

代码示例来源: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. if (tableRule.isPresent()) {
  7. if (!bindingTableNames.contains(each)) {
  8. result.add(new StandardRoutingEngine(shardingRule, tableRule.get().getLogicTable(), shardingConditions).route());
  9. if (result.isEmpty()) {
  10. throw new ShardingException("Cannot find table rule and default data source with logic tables: '%s'", logicTables);
  11. if (1 == result.size()) {
  12. return result.iterator().next();

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

  1. public Collection<V> values() {
  2. processQueue();
  3. Collection<K> keys = map.keySet();
  4. if (keys.isEmpty()) {
  5. //noinspection unchecked
  6. return Collections.EMPTY_SET;
  7. }
  8. Collection<V> values = new ArrayList<V>(keys.size());
  9. for (K key : keys) {
  10. V v = get(key);
  11. if (v != null) {
  12. values.add(v);
  13. }
  14. }
  15. return values;
  16. }

代码示例来源:origin: stagemonitor/stagemonitor

  1. private static void initIncludesAndExcludes() {
  2. CorePlugin corePlugin = Stagemonitor.getPlugin(CorePlugin.class);
  3. excludeContaining = new ArrayList<String>(corePlugin.getExcludeContaining().size());
  4. excludeContaining.addAll(corePlugin.getExcludeContaining());
  5. excludes = new ArrayList<String>(corePlugin.getExcludePackages().size());
  6. excludes.add("org.stagemonitor");
  7. excludes.addAll(corePlugin.getExcludePackages());
  8. includes = new ArrayList<String>(corePlugin.getIncludePackages().size());
  9. includes.addAll(corePlugin.getIncludePackages());
  10. if (includes.isEmpty()) {
  11. logger.warn("No includes for instrumentation configured. Please set the stagemonitor.instrument.include property.");
  12. }
  13. }

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

  1. @Nullable
  2. private Collection<CacheOperation> parseCacheAnnotations(
  3. DefaultCacheConfig cachingConfig, AnnotatedElement ae, boolean localOnly) {
  4. Collection<? extends Annotation> anns = (localOnly ?
  5. AnnotatedElementUtils.getAllMergedAnnotations(ae, CACHE_OPERATION_ANNOTATIONS) :
  6. AnnotatedElementUtils.findAllMergedAnnotations(ae, CACHE_OPERATION_ANNOTATIONS));
  7. if (anns.isEmpty()) {
  8. return null;
  9. }
  10. final Collection<CacheOperation> ops = new ArrayList<>(1);
  11. anns.stream().filter(ann -> ann instanceof Cacheable).forEach(
  12. ann -> ops.add(parseCacheableAnnotation(ae, cachingConfig, (Cacheable) ann)));
  13. anns.stream().filter(ann -> ann instanceof CacheEvict).forEach(
  14. ann -> ops.add(parseEvictAnnotation(ae, cachingConfig, (CacheEvict) ann)));
  15. anns.stream().filter(ann -> ann instanceof CachePut).forEach(
  16. ann -> ops.add(parsePutAnnotation(ae, cachingConfig, (CachePut) ann)));
  17. anns.stream().filter(ann -> ann instanceof Caching).forEach(
  18. ann -> parseCachingAnnotation(ae, cachingConfig, (Caching) ann, ops));
  19. return ops;
  20. }

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

  1. /**
  2. * Gets size of the given collection with provided optional predicates.
  3. *
  4. * @param c Collection to size.
  5. * @param p Optional predicates that filters out elements from count.
  6. * @param <T> Type of the iterator.
  7. * @return Number of elements in the collection for which all given predicates
  8. * evaluates to {@code true}. If no predicates is provided - all elements are counted.
  9. */
  10. public static <T> int size(@Nullable Collection<? extends T> c, @Nullable IgnitePredicate<? super T>... p) {
  11. return c == null || c.isEmpty() ? 0 : isEmpty(p) || isAlwaysTrue(p) ? c.size() : size(c.iterator(), p);
  12. }

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

  1. @CanIgnoreReturnValue
  2. @Override
  3. public boolean putAll(@Nullable K key, Iterable<? extends V> values) {
  4. checkNotNull(values);
  5. // make sure we only call values.iterator() once
  6. // and we only call get(key) if values is nonempty
  7. if (values instanceof Collection) {
  8. Collection<? extends V> valueCollection = (Collection<? extends V>) values;
  9. return !valueCollection.isEmpty() && get(key).addAll(valueCollection);
  10. } else {
  11. Iterator<? extends V> valueItr = values.iterator();
  12. return valueItr.hasNext() && Iterators.addAll(get(key), valueItr);
  13. }
  14. }

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

  1. /** {@inheritDoc} */
  2. @Override public GridClientData pinNodes(GridClientNode node, GridClientNode... nodes) throws GridClientException {
  3. Collection<GridClientNode> pinnedNodes = new ArrayList<>(nodes != null ? nodes.length + 1 : 1);
  4. if (node != null)
  5. pinnedNodes.add(node);
  6. if (nodes != null && nodes.length != 0)
  7. pinnedNodes.addAll(Arrays.asList(nodes));
  8. return createProjection(pinnedNodes.isEmpty() ? null : pinnedNodes,
  9. null, null, new GridClientDataFactory(flags));
  10. }

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

  1. /** An implementation of {@link Multiset#addAll}. */
  2. static <E> boolean addAllImpl(Multiset<E> self, Collection<? extends E> elements) {
  3. checkNotNull(self);
  4. checkNotNull(elements);
  5. if (elements instanceof Multiset) {
  6. return addAllImpl(self, cast(elements));
  7. } else if (elements.isEmpty()) {
  8. return false;
  9. } else {
  10. return Iterators.addAll(self, elements.iterator());
  11. }
  12. }

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

  1. @Override
  2. public RFuture<Boolean> addAllAsync(Collection<? extends String> c) {
  3. if (c.isEmpty()) {
  4. return RedissonPromise.newSucceededFuture(false);
  5. }
  6. List<Object> params = new ArrayList<Object>(2*c.size());
  7. params.add(getName());
  8. for (Object param : c) {
  9. params.add(0);
  10. params.add(param);
  11. }
  12. return commandExecutor.writeAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZADD_BOOL_RAW, params.toArray());
  13. }

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

  1. @Override
  2. public final int getTransactionIsolation() throws SQLException {
  3. if (cachedConnections.values().isEmpty()) {
  4. return transactionIsolation;
  5. }
  6. return cachedConnections.values().iterator().next().getTransactionIsolation();
  7. }

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

  1. } else if (elements instanceof Collection) {
  2. Collection<E> collection = (Collection<E>) elements;
  3. if (collection.isEmpty()) {
  4. return ImmutableSet.of();
  5. } else {
  6. if (itr.hasNext()) {
  7. EnumSet<E> enumSet = EnumSet.of(itr.next());
  8. Iterators.addAll(enumSet, itr);
  9. return ImmutableEnumSet.asImmutable(enumSet);

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

  1. /**
  2. * Load proxy configuration.
  3. *
  4. * @return proxy configuration
  5. * @throws IOException IO exception
  6. */
  7. public ShardingConfiguration load() throws IOException {
  8. Collection<String> schemaNames = new HashSet<>();
  9. YamlProxyServerConfiguration serverConfig = loadServerConfiguration(new File(ShardingConfigurationLoader.class.getResource(CONFIG_PATH + SERVER_CONFIG_FILE).getFile()));
  10. File configPath = new File(ShardingConfigurationLoader.class.getResource(CONFIG_PATH).getFile());
  11. Collection<YamlProxyRuleConfiguration> ruleConfigurations = new LinkedList<>();
  12. for (File each : findRuleConfigurationFiles(configPath)) {
  13. Optional<YamlProxyRuleConfiguration> ruleConfig = loadRuleConfiguration(each, serverConfig);
  14. if (ruleConfig.isPresent()) {
  15. Preconditions.checkState(schemaNames.add(ruleConfig.get().getSchemaName()), "Schema name `%s` must unique at all rule configurations.", ruleConfig.get().getSchemaName());
  16. ruleConfigurations.add(ruleConfig.get());
  17. }
  18. }
  19. Preconditions.checkState(!ruleConfigurations.isEmpty() || null != serverConfig.getOrchestration(), "Can not find any sharding rule configuration file in path `%s`.", configPath.getPath());
  20. Map<String, YamlProxyRuleConfiguration> ruleConfigurationMap = new HashMap<>(ruleConfigurations.size(), 1);
  21. for (YamlProxyRuleConfiguration each : ruleConfigurations) {
  22. ruleConfigurationMap.put(each.getSchemaName(), each);
  23. }
  24. return new ShardingConfiguration(serverConfig, ruleConfigurationMap);
  25. }

相关文章