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

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

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

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

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

/** Used during deserialization only. */
final void setMap(Map<K, Collection<V>> map) {
 this.map = map;
 totalSize = 0;
 for (Collection<V> values : map.values()) {
  checkArgument(!values.isEmpty());
  totalSize += values.size();
 }
}

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

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

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

RoutingResult result = new RoutingResult();
if (shardingRule.isAllBroadcastTables(logicTables)) {
  List<RoutingTable> routingTables = new ArrayList<>(logicTables.size());
  for (String each : logicTables) {
    routingTables.add(new RoutingTable(each, each));
  result.getTableUnits().getTableUnits().add(tableUnit);
} else if (logicTables.isEmpty()) {
  result.getTableUnits().getTableUnits().add(new TableUnit(shardingRule.getShardingDataSourceNames().getRandomDataSourceName()));
} else if (1 == logicTables.size()) {
  String logicTableName = logicTables.iterator().next();
  DataNode dataNode = shardingRule.getDataNode(logicTableName);
  TableUnit tableUnit = new TableUnit(dataNode.getDataSourceName());
  tableUnit.getRoutingTables().add(new RoutingTable(logicTableName, dataNode.getTableName()));
  result.getTableUnits().getTableUnits().add(tableUnit);
} else {
  List<RoutingTable> routingTables = new ArrayList<>(logicTables.size());
  Set<String> availableDatasourceNames = null;
  boolean first = true;
    TableRule tableRule = shardingRule.getTableRule(each);
    DataNode dataNode = tableRule.getActualDataNodes().get(0);
    routingTables.add(new RoutingTable(each, dataNode.getTableName()));
    Set<String> currentDataSourceNames = new HashSet<>(tableRule.getActualDatasourceNames().size());
    for (DataNode eachDataNode : tableRule.getActualDataNodes()) {

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

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

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

public boolean removeAll(Collection coll) {
  if (parent.isEmpty() || coll.isEmpty()) {
    return false;
  }
  boolean modified = false;
  Iterator it = iterator();
  while (it.hasNext()) {
    if (coll.contains(it.next())) {
      it.remove();
      modified = true;
    }
  }
  return modified;
}

代码示例来源: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) {
    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: apache/shiro

public Collection<V> values() {
  processQueue();
  Collection<K> keys = map.keySet();
  if (keys.isEmpty()) {
    //noinspection unchecked
    return Collections.EMPTY_SET;
  }
  Collection<V> values = new ArrayList<V>(keys.size());
  for (K key : keys) {
    V v = get(key);
    if (v != null) {
      values.add(v);
    }
  }
  return values;
}

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

private static void initIncludesAndExcludes() {
  CorePlugin corePlugin = Stagemonitor.getPlugin(CorePlugin.class);
  excludeContaining = new ArrayList<String>(corePlugin.getExcludeContaining().size());
  excludeContaining.addAll(corePlugin.getExcludeContaining());
  excludes = new ArrayList<String>(corePlugin.getExcludePackages().size());
  excludes.add("org.stagemonitor");
  excludes.addAll(corePlugin.getExcludePackages());
  includes = new ArrayList<String>(corePlugin.getIncludePackages().size());
  includes.addAll(corePlugin.getIncludePackages());
  if (includes.isEmpty()) {
    logger.warn("No includes for instrumentation configured. Please set the stagemonitor.instrument.include property.");
  }
}

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

@Nullable
private Collection<CacheOperation> parseCacheAnnotations(
    DefaultCacheConfig cachingConfig, AnnotatedElement ae, boolean localOnly) {
  Collection<? extends Annotation> anns = (localOnly ?
      AnnotatedElementUtils.getAllMergedAnnotations(ae, CACHE_OPERATION_ANNOTATIONS) :
      AnnotatedElementUtils.findAllMergedAnnotations(ae, CACHE_OPERATION_ANNOTATIONS));
  if (anns.isEmpty()) {
    return null;
  }
  final Collection<CacheOperation> ops = new ArrayList<>(1);
  anns.stream().filter(ann -> ann instanceof Cacheable).forEach(
      ann -> ops.add(parseCacheableAnnotation(ae, cachingConfig, (Cacheable) ann)));
  anns.stream().filter(ann -> ann instanceof CacheEvict).forEach(
      ann -> ops.add(parseEvictAnnotation(ae, cachingConfig, (CacheEvict) ann)));
  anns.stream().filter(ann -> ann instanceof CachePut).forEach(
      ann -> ops.add(parsePutAnnotation(ae, cachingConfig, (CachePut) ann)));
  anns.stream().filter(ann -> ann instanceof Caching).forEach(
      ann -> parseCachingAnnotation(ae, cachingConfig, (Caching) ann, ops));
  return ops;
}

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

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

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

@CanIgnoreReturnValue
@Override
public boolean putAll(@Nullable K key, Iterable<? extends V> values) {
 checkNotNull(values);
 // make sure we only call values.iterator() once
 // and we only call get(key) if values is nonempty
 if (values instanceof Collection) {
  Collection<? extends V> valueCollection = (Collection<? extends V>) values;
  return !valueCollection.isEmpty() && get(key).addAll(valueCollection);
 } else {
  Iterator<? extends V> valueItr = values.iterator();
  return valueItr.hasNext() && Iterators.addAll(get(key), valueItr);
 }
}

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

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

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

/** An implementation of {@link Multiset#addAll}. */
static <E> boolean addAllImpl(Multiset<E> self, Collection<? extends E> elements) {
 checkNotNull(self);
 checkNotNull(elements);
 if (elements instanceof Multiset) {
  return addAllImpl(self, cast(elements));
 } else if (elements.isEmpty()) {
  return false;
 } else {
  return Iterators.addAll(self, elements.iterator());
 }
}

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

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

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

@Override
public final int getTransactionIsolation() throws SQLException {
  if (cachedConnections.values().isEmpty()) {
    return transactionIsolation;
  }
  return cachedConnections.values().iterator().next().getTransactionIsolation();
}

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

} else if (elements instanceof Collection) {
 Collection<E> collection = (Collection<E>) elements;
 if (collection.isEmpty()) {
  return ImmutableSet.of();
 } else {
 if (itr.hasNext()) {
  EnumSet<E> enumSet = EnumSet.of(itr.next());
  Iterators.addAll(enumSet, itr);
  return ImmutableEnumSet.asImmutable(enumSet);

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

/**
 * Load proxy configuration.
 *
 * @return proxy configuration
 * @throws IOException IO exception
 */
public ShardingConfiguration load() throws IOException {
  Collection<String> schemaNames = new HashSet<>();
  YamlProxyServerConfiguration serverConfig = loadServerConfiguration(new File(ShardingConfigurationLoader.class.getResource(CONFIG_PATH + SERVER_CONFIG_FILE).getFile()));
  File configPath = new File(ShardingConfigurationLoader.class.getResource(CONFIG_PATH).getFile());
  Collection<YamlProxyRuleConfiguration> ruleConfigurations = new LinkedList<>();
  for (File each : findRuleConfigurationFiles(configPath)) {
    Optional<YamlProxyRuleConfiguration> ruleConfig = loadRuleConfiguration(each, serverConfig);
    if (ruleConfig.isPresent()) {
      Preconditions.checkState(schemaNames.add(ruleConfig.get().getSchemaName()), "Schema name `%s` must unique at all rule configurations.", ruleConfig.get().getSchemaName());
      ruleConfigurations.add(ruleConfig.get());
    }
  }
  Preconditions.checkState(!ruleConfigurations.isEmpty() || null != serverConfig.getOrchestration(), "Can not find any sharding rule configuration file in path `%s`.", configPath.getPath());
  Map<String, YamlProxyRuleConfiguration> ruleConfigurationMap = new HashMap<>(ruleConfigurations.size(), 1);
  for (YamlProxyRuleConfiguration each : ruleConfigurations) {
    ruleConfigurationMap.put(each.getSchemaName(), each);
  }
  return new ShardingConfiguration(serverConfig, ruleConfigurationMap);
}

相关文章