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

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

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

Set.isEmpty介绍

[英]Returns true if this set has no elements.
[中]如果此集合没有元素,则返回true。

代码示例

代码示例来源:origin: iluwatar/java-design-patterns

/**
 * Checkout object from pool
 */
public synchronized T checkOut() {
 if (available.isEmpty()) {
  available.add(create());
 }
 T instance = available.iterator().next();
 available.remove(instance);
 inUse.add(instance);
 return instance;
}

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

/**
 * returns the first partition (out of {@link #partitions}) for which no offset is defined.
 * @deprecated please use {@link #partitions}
 * @return a partition with no offset
 */
@Deprecated
public TopicPartition partition() {
  return partitions.isEmpty() ? null : partitions.iterator().next();
}

代码示例来源:origin: prestodb/presto

private static Set<String> _merge(Set<String> s1, Set<String> s2)
{
  if (s1.isEmpty()) {
    return s2;
  } else if (s2.isEmpty()) {
    return s1;
  }
  HashSet<String> result = new HashSet<String>(s1.size() + s2.size());
  result.addAll(s1);
  result.addAll(s2);
  return result;
}

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

private static Set<HttpMethod> initAllowedHttpMethods(Set<HttpMethod> declaredMethods) {
  if (declaredMethods.isEmpty()) {
    return EnumSet.allOf(HttpMethod.class).stream()
        .filter(method -> method != HttpMethod.TRACE)
        .collect(Collectors.toSet());
  }
  else {
    Set<HttpMethod> result = new LinkedHashSet<>(declaredMethods);
    if (result.contains(HttpMethod.GET)) {
      result.add(HttpMethod.HEAD);
    }
    result.add(HttpMethod.OPTIONS);
    return result;
  }
}

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

private static Set<HttpMethod> initAllowedHttpMethods(Set<String> declaredMethods) {
  Set<HttpMethod> result = new LinkedHashSet<>(declaredMethods.size());
  if (declaredMethods.isEmpty()) {
    for (HttpMethod method : HttpMethod.values()) {
      if (method != HttpMethod.TRACE) {
        result.add(method);
      }
    }
  }
  else {
    for (String method : declaredMethods) {
      HttpMethod httpMethod = HttpMethod.valueOf(method);
      result.add(httpMethod);
      if (httpMethod == HttpMethod.GET) {
        result.add(HttpMethod.HEAD);
      }
    }
    result.add(HttpMethod.OPTIONS);
  }
  return result;
}

代码示例来源:origin: alibaba/cobar

/**
   * @param orig if null, return intersect
   */
  public static Set<? extends Object> intersectSet(Set<? extends Object> orig, Set<? extends Object> intersect) {
    if (orig == null)
      return intersect;
    if (intersect == null || orig.isEmpty())
      return Collections.emptySet();
    Set<Object> set = new HashSet<Object>(orig.size());
    for (Object p : orig) {
      if (intersect.contains(p))
        set.add(p);
    }
    return set;
  }
}

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

@Override
public Enumeration<String> getParameterNames() {
  if (this.multipartParameterNames == null) {
    initializeMultipart();
  }
  if (this.multipartParameterNames.isEmpty()) {
    return super.getParameterNames();
  }
  // Servlet 3.0 getParameterNames() not guaranteed to include multipart form items
  // (e.g. on WebLogic 12) -> need to merge them here to be on the safe side
  Set<String> paramNames = new LinkedHashSet<>();
  Enumeration<String> paramEnum = super.getParameterNames();
  while (paramEnum.hasMoreElements()) {
    paramNames.add(paramEnum.nextElement());
  }
  paramNames.addAll(this.multipartParameterNames);
  return Collections.enumeration(paramNames);
}

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

private Map<String, String> filterMdc(Map<String, String> mdcPropertyMap) {
  if (includesMdcKeys.isEmpty()) {
    return mdcPropertyMap;
  }
  return mdcPropertyMap.entrySet()
    .stream()
    .filter(e -> includesMdcKeys.contains(e.getKey()))
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

代码示例来源:origin: alibaba/druid

private void loadNameList() {
  Set<String> names = new HashSet<String>();
  for (String n : properties.stringPropertyNames()) {
    if (n.contains(".url")) {
      names.add(n.split("\\.url")[0]);
    }
  }
  if (!names.isEmpty()) {
    nameList.addAll(names);
  }
}

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

@Test
public void testOneDistanceIteration() {
  Iterator<Vector3i> iter = Diamond3iIterator.iterate(Vector3i.zero(), 1).iterator();
  Set<Vector3i> expected = Sets.newHashSet(Vector3i.zero(), new Vector3i(1, 0, 0), new Vector3i(-1, 0, 0), new Vector3i(0, 1, 0),
      new Vector3i(0, -1, 0), new Vector3i(0, 0, 1), new Vector3i(0, 0, -1));
  while (iter.hasNext()) {
    Vector3i next = iter.next();
    assertTrue("Received Unexpected: " + next, expected.remove(next));
  }
  assertTrue("Missing: " + expected, expected.isEmpty());
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testRegister() {
 assertTrue(vertx.verticleFactories().isEmpty());
 VerticleFactory fact1 = new TestVerticleFactory("foo");
 vertx.registerVerticleFactory(fact1);
 assertEquals(1, vertx.verticleFactories().size());
 assertTrue(vertx.verticleFactories().contains(fact1));
}

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

@Override
public PersistenceUnitInfo obtainDefaultPersistenceUnitInfo() {
  if (this.persistenceUnitInfoNames.isEmpty()) {
    throw new IllegalStateException("No persistence units parsed from " +
        ObjectUtils.nullSafeToString(this.persistenceXmlLocations));
  }
  if (this.persistenceUnitInfos.isEmpty()) {
    throw new IllegalStateException("All persistence units from " +
        ObjectUtils.nullSafeToString(this.persistenceXmlLocations) + " already obtained");
  }
  if (this.persistenceUnitInfos.size() > 1 && this.defaultPersistenceUnitName != null) {
    return obtainPersistenceUnitInfo(this.defaultPersistenceUnitName);
  }
  PersistenceUnitInfo pui = this.persistenceUnitInfos.values().iterator().next();
  this.persistenceUnitInfos.clear();
  return pui;
}

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

private int getParallelism(List<ProcessorNode> group) {
  Set<Integer> parallelisms = group.stream().map(Node::getParallelism).collect(Collectors.toSet());
  if (parallelisms.size() > 1) {
    throw new IllegalStateException("Current group does not have same parallelism " + group);
  }
  return parallelisms.isEmpty() ? 1 : parallelisms.iterator().next();
}

代码示例来源:origin: org.assertj/assertj-core

/**
 * Checks that the {@code expectedNames} are part of the {@code actualNames}. If an {@code expectedName} is not
 * contained in the {@code actualNames}, the this method will return {@code true}. THe {@code missingNames} will
 * contain all the {@code expectedNames} that are not part of the {@code actualNames}.
 *
 * @param actualNames the names that should be used to check
 * @param expectedNames the names that should be contained in {@code actualNames}
 * @param missingNames the names that were not part of {@code expectedNames}
 * @return {@code true} if all {@code expectedNames} are part of the {@code actualNames}, {@code false} otherwise
 */
private static boolean noMissingElement(Set<String> actualNames, Set<String> expectedNames,
                    Set<String> missingNames) {
 for (String field : expectedNames) {
  if (!actualNames.contains(field)) missingNames.add(field);
 }
 return missingNames.isEmpty();
}

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

@Implementation
protected @Nullable String[] getPackagesForUid(int uid) {
 String[] packageNames = packagesForUid.get(uid);
 if (packageNames != null) {
  return packageNames;
 }
 Set<String> results = new HashSet<>();
 for (PackageInfo packageInfo : packageInfos.values()) {
  if (packageInfo.applicationInfo != null && packageInfo.applicationInfo.uid == uid) {
   results.add(packageInfo.packageName);
  }
 }
 return results.isEmpty() ? null : results.toArray(new String[results.size()]);
}

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

@Override
@Nullable
public String getPrefix(String namespaceUri) {
  Set<String> prefixes = getPrefixesSet(namespaceUri);
  return (!prefixes.isEmpty() ? prefixes.iterator().next() : null);
}

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

@Test
public void testBlobSynchronizerForKeysToDownload() {
 BlobStore store = initLocalFs();
 LocalFsBlobStoreSynchronizer sync = new LocalFsBlobStoreSynchronizer(store, conf);
 // test for keylist to download
 Set<String> zkSet = new HashSet<String>();
 zkSet.add("key1");
 Set<String> blobStoreSet = new HashSet<String>();
 blobStoreSet.add("key1");
 Set<String> resultSet = sync.getKeySetToDownload(blobStoreSet, zkSet);
 assertTrue("Not Empty", resultSet.isEmpty());
 zkSet.add("key1");
 blobStoreSet.add("key2");
 resultSet =  sync.getKeySetToDownload(blobStoreSet, zkSet);
 assertTrue("Not Empty", resultSet.isEmpty());
 blobStoreSet.remove("key1");
 blobStoreSet.remove("key2");
 zkSet.add("key1");
 resultSet =  sync.getKeySetToDownload(blobStoreSet, zkSet);
 assertTrue("Unexpected keys to download", (resultSet.size() == 1) && (resultSet.contains("key1")));
}

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

private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
  AnnotationAttributes attributes = AnnotationAttributes.fromMap(
      metadata.getAnnotationAttributes(DubboComponentScan.class.getName()));
  String[] basePackages = attributes.getStringArray("basePackages");
  Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
  String[] value = attributes.getStringArray("value");
  // Appends value array attributes
  Set<String> packagesToScan = new LinkedHashSet<String>(Arrays.asList(value));
  packagesToScan.addAll(Arrays.asList(basePackages));
  for (Class<?> basePackageClass : basePackageClasses) {
    packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
  }
  if (packagesToScan.isEmpty()) {
    return Collections.singleton(ClassUtils.getPackageName(metadata.getClassName()));
  }
  return packagesToScan;
}

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

private Map<String, String> filterHeaders(Map<String, String> headers, Set<String> filteredHeaderNames) {
  if (filteredHeaderNames.isEmpty()) {
    return Collections.emptyMap();
  }
  return headers.entrySet().stream()
    .filter(e -> filteredHeaderNames.contains(e.getKey()))
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

代码示例来源:origin: skylot/jadx

/**
 * Add exception type to catch block
 * @param type - null for 'all' or 'Throwable' handler
 */
public void addCatchType(@Nullable ClassInfo type) {
  if (type != null) {
    this.catchTypes.add(type);
  } else {
    if (!this.catchTypes.isEmpty()) {
      throw new JadxRuntimeException("Null type added to not empty exception handler: " + this);
    }
  }
}

相关文章