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

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

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

Set.size介绍

[英]Returns the number of elements in this set.
[中]返回此集合中的元素数。

代码示例

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

@Override
public int size() {
 int size = set1.size();
 for (E e : set2) {
  if (!set1.contains(e)) {
   size++;
  }
 }
 return size;
}

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

public Class[] getInterfaces() {
  return interfaces.toArray(new Class[this.interfaces.size()]);
}

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

public static Method[] getPropertyMethods(PropertyDescriptor[] properties, boolean read, boolean write) {
  Set methods = new HashSet();
  for (int i = 0; i < properties.length; i++) {
    PropertyDescriptor pd = properties[i];
    if (read) {
      methods.add(pd.getReadMethod());
    }
    if (write) {
      methods.add(pd.getWriteMethod());
    }
  }
  methods.remove(null);
  return (Method[]) methods.toArray(new Method[methods.size()]);
}

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

private static boolean fieldsEquals(RecordCursor cursor, Set<Field> fields)
{
  if (fields.size() < 2) {
    return true; // Nothing to compare
  }
  Iterator<Field> fieldIterator = fields.iterator();
  Field firstField = fieldIterator.next();
  while (fieldIterator.hasNext()) {
    if (!fieldEquals(cursor, firstField, fieldIterator.next())) {
      return false;
    }
  }
  return true;
}

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

private void doTestMultipartHttpServletRequest(MultipartHttpServletRequest request) throws IOException {
  Set<String> fileNames = new HashSet<>();
  Iterator<String> fileIter = request.getFileNames();
  while (fileIter.hasNext()) {
    fileNames.add(fileIter.next());
  }
  assertEquals(2, fileNames.size());
  assertTrue(fileNames.contains("file1"));
  assertTrue(fileNames.contains("file2"));
  MultipartFile file1 = request.getFile("file1");
  MultipartFile file2 = request.getFile("file2");
  Map<String, MultipartFile> fileMap = request.getFileMap();
  List<String> fileMapKeys = new LinkedList<>(fileMap.keySet());
  assertEquals(2, fileMapKeys.size());
  assertEquals(file1, fileMap.get("file1"));
  assertEquals(file2, fileMap.get("file2"));
  assertEquals("file1", file1.getName());
  assertEquals("", file1.getOriginalFilename());
  assertNull(file1.getContentType());
  assertTrue(ObjectUtils.nullSafeEquals("myContent1".getBytes(), file1.getBytes()));
  assertTrue(ObjectUtils.nullSafeEquals("myContent1".getBytes(),
    FileCopyUtils.copyToByteArray(file1.getInputStream())));
  assertEquals("file2", file2.getName());
  assertEquals("myOrigFilename", file2.getOriginalFilename());
  assertEquals("text/plain", file2.getContentType());
  assertTrue(ObjectUtils.nullSafeEquals("myContent2".getBytes(), file2.getBytes()));
  assertTrue(ObjectUtils.nullSafeEquals("myContent2".getBytes(),
    FileCopyUtils.copyToByteArray(file2.getInputStream())));
}

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

@Override
  public String toString() {
    if (this.httpMethods.size() == 1) {
      return this.httpMethods.iterator().next().toString();
    }
    else {
      return this.httpMethods.toString();
    }
  }
}

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

static char[] randomChars(Random rand, int size) {
 Set<Character> chars = new HashSet<>(size);
 for (int i = 0; i < size; i++) {
  char c;
  while (true) {
   c = (char) rand.nextInt(Character.MAX_VALUE - Character.MIN_VALUE + 1);
   if (!chars.contains(c)) {
    break;
   }
  }
  chars.add(c);
 }
 char[] retValue = new char[chars.size()];
 int i = 0;
 for (char c : chars) {
  retValue[i++] = c;
 }
 Arrays.sort(retValue);
 return retValue;
}

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

@Override
public File[] listFiles( File directory )
{
  directory = canonicalFile( directory );
  if ( files.containsKey( directory ) || !directories.contains( directory ) )
  {
    // This means that you're trying to list files on a file, not a directory.
    return null;
  }
  List<String> directoryPathItems = splitPath( directory );
  Set<File> found = new HashSet<>();
  Iterator<File> files = new CombiningIterator<>( asList( this.files.keySet().iterator(), directories.iterator() ) );
  while ( files.hasNext() )
  {
    File file = files.next();
    List<String> fileNamePathItems = splitPath( file );
    if ( directoryMatches( directoryPathItems, fileNamePathItems ) )
    {
      found.add( constructPath( fileNamePathItems, directoryPathItems ) );
    }
  }
  return found.toArray( new File[found.size()] );
}

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

private Set<String> resolvePackagesToScan(Set<String> packagesToScan) {
  Set<String> resolvedPackagesToScan = new LinkedHashSet<String>(packagesToScan.size());
  for (String packageToScan : packagesToScan) {
    if (StringUtils.hasText(packageToScan)) {
      String resolvedPackageToScan = environment.resolvePlaceholders(packageToScan.trim());
      resolvedPackagesToScan.add(resolvedPackageToScan);
    }
  }
  return resolvedPackagesToScan;
}

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

/**
 * Returns a new proxy for {@code interfaceType}. Proxies of the same interface are equal to each
 * other if the {@link DummyProxy} instance that created the proxies are equal.
 */
final <T> T newProxy(TypeToken<T> interfaceType) {
 Set<Class<?>> interfaceClasses = Sets.newLinkedHashSet();
 interfaceClasses.addAll(interfaceType.getTypes().interfaces().rawTypes());
 // Make the proxy serializable to work with SerializableTester
 interfaceClasses.add(Serializable.class);
 Object dummy =
   Proxy.newProxyInstance(
     interfaceClasses.iterator().next().getClassLoader(),
     interfaceClasses.toArray(new Class<?>[interfaceClasses.size()]),
     new DummyHandler(interfaceType));
 @SuppressWarnings("unchecked") // interfaceType is T
 T result = (T) dummy;
 return result;
}

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

@Test
public void testLinger() throws Exception {
  long lingerMs = 10L;
  RecordAccumulator accum = createTestRecordAccumulator(
      1024 + DefaultRecordBatch.RECORD_BATCH_OVERHEAD, 10 * 1024, CompressionType.NONE, lingerMs);
  accum.append(tp1, 0L, key, value, Record.EMPTY_HEADERS, null, maxBlockTimeMs);
  assertEquals("No partitions should be ready", 0, accum.ready(cluster, time.milliseconds()).readyNodes.size());
  time.sleep(10);
  assertEquals("Our partition's leader should be ready", Collections.singleton(node1), accum.ready(cluster, time.milliseconds()).readyNodes);
  List<ProducerBatch> batches = accum.drain(cluster, Collections.singleton(node1), Integer.MAX_VALUE, 0).get(node1.id());
  assertEquals(1, batches.size());
  ProducerBatch batch = batches.get(0);
  Iterator<Record> iter = batch.records().records().iterator();
  Record record = iter.next();
  assertEquals("Keys should match", ByteBuffer.wrap(key), record.key());
  assertEquals("Values should match", ByteBuffer.wrap(value), record.value());
  assertFalse("No more records", iter.hasNext());
}

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

Enumeration<String> parameterEnum = request.getParameterNames();
while (parameterEnum.hasMoreElements()) {
  parameterNames.add(parameterEnum.nextElement());
assertEquals(3, parameterNames.size());
assertTrue(parameterNames.contains("field3"));
assertTrue(parameterNames.contains("field4"));
assertTrue(parameterNames.contains("getField"));
assertEquals("value3", request.getParameter("field3"));
List<String> parameterValues = Arrays.asList(request.getParameterValues("field3"));
  String key = (String) o;
  parameterMapKeys.add(key);
  parameterMapValues.add(request.getParameterMap().get(key));

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

@Test
@Graph( { "a TO b", "b TO c", "c TO a" } )
public void canCreateGraphFromMultipleStrings()
{
  Map<String,Node> graph = data.get();
  Set<Node> unique = new HashSet<>();
  Node n = graph.get( "a" );
  while ( unique.add( n ) )
  {
    try ( Transaction ignored = graphdb.beginTx() )
    {
      n = n.getSingleRelationship( RelationshipType.withName( "TO" ), Direction.OUTGOING ).getEndNode();
    }
  }
  assertEquals( graph.size(), unique.size() );
}

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

public void noMoreSplits(PlanNodeId planNodeId)
{
  if (noMoreSplits.contains(planNodeId)) {
    return;
  }
  noMoreSplits.add(planNodeId);
  if (noMoreSplits.size() < sourceStartOrder.size()) {
    return;
  }
  checkState(noMoreSplits.size() == sourceStartOrder.size());
  checkState(noMoreSplits.containsAll(sourceStartOrder));
  status.setNoMoreLifespans();
}

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

@Test
public void testPopulatedSet() throws Exception {
  HasMap hasMap = (HasMap) this.beanFactory.getBean("set");
  assertTrue(hasMap.getSet().size() == 3);
  assertTrue(hasMap.getSet().contains("bar"));
  TestBean jenny = (TestBean) this.beanFactory.getBean("jenny");
  assertTrue(hasMap.getSet().contains(jenny));
  assertTrue(hasMap.getSet().contains(null));
  Iterator it = hasMap.getSet().iterator();
  assertEquals("bar", it.next());
  assertEquals(jenny, it.next());
  assertEquals(null, it.next());
}

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

public void testEmptyRangeSubMultiset(SortedMultiset<E> multiset) {
 assertTrue(multiset.isEmpty());
 assertEquals(0, multiset.size());
 assertEquals(0, multiset.toArray().length);
 assertTrue(multiset.entrySet().isEmpty());
 assertFalse(multiset.iterator().hasNext());
 assertEquals(0, multiset.entrySet().size());
 assertEquals(0, multiset.entrySet().toArray().length);
 assertFalse(multiset.entrySet().iterator().hasNext());
}

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

private static void assertEmpty(Set<? extends List<?>> set) {
 assertTrue(set.isEmpty());
 assertEquals(0, set.size());
 assertFalse(set.iterator().hasNext());
}

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

@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests
@Test
public void setCollectionPropertyWithArrayValue() {
  IndexedTestBean target = new IndexedTestBean();
  AbstractPropertyAccessor accessor = createAccessor(target);
  Collection<String> coll = new HashSet<>();
  coll.add("coll1");
  accessor.setPropertyValue("collection", coll.toArray());
  List<String> set = new LinkedList<>();
  set.add("set1");
  accessor.setPropertyValue("set", set.toArray());
  List<String> sortedSet = new ArrayList<>();
  sortedSet.add("sortedSet1");
  accessor.setPropertyValue("sortedSet", sortedSet.toArray());
  Set<String> list = new HashSet<>();
  list.add("list1");
  accessor.setPropertyValue("list", list.toArray());
  assertEquals(1, target.getCollection().size());
  assertTrue(target.getCollection().containsAll(coll));
  assertEquals(1, target.getSet().size());
  assertTrue(target.getSet().containsAll(set));
  assertEquals(1, target.getSortedSet().size());
  assertTrue(target.getSortedSet().containsAll(sortedSet));
  assertEquals(1, target.getList().size());
  assertTrue(target.getList().containsAll(list));
}

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

@Override
 TypeVariable<?> captureAsTypeVariable(Type[] upperBounds) {
  Set<Type> combined = new LinkedHashSet<>(asList(upperBounds));
  // Since this is an artifically generated type variable, we don't bother checking
  // subtyping between declared type bound and actual type bound. So it's possible that we
  // may generate something like <capture#1-of ? extends Foo&SubFoo>.
  // Checking subtype between declared and actual type bounds
  // adds recursive isSubtypeOf() call and feels complicated.
  // There is no contract one way or another as long as isSubtypeOf() works as expected.
  combined.addAll(asList(typeParam.getBounds()));
  if (combined.size() > 1) { // Object is implicit and only useful if it's the only bound.
   combined.remove(Object.class);
  }
  return super.captureAsTypeVariable(combined.toArray(new Type[0]));
 }
};

相关文章