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

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

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

Collection.toArray介绍

[英]Returns a new array containing all elements contained in this Collection. If the implementation has ordered elements it will return the element array in the same order as an iterator would return them. The array returned does not reflect any changes of the Collection. A new array is created even if the underlying data structure is already an array.
[中]返回包含此集合中包含的所有元素的新数组。如果实现对元素进行了排序,它将以迭代器返回元素数组的相同顺序返回元素数组。返回的数组不反映集合的任何更改。即使基础数据结构已经是一个数组,也会创建一个新数组。

代码示例

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

  1. private static Type[] toArray(Collection<Type> types) {
  2. return types.toArray(new Type[types.size()]);
  3. }

代码示例来源:origin: hankcs/HanLP

  1. @Override
  2. public <T> T[] toArray(T[] a)
  3. {
  4. return termFrequencyMap.values().toArray(a);
  5. }

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

  1. /**
  2. * Gets the read-only list of all {@link Computer}s.
  3. */
  4. public Computer[] getComputers() {
  5. Computer[] r = computers.values().toArray(new Computer[computers.size()]);
  6. Arrays.sort(r,new Comparator<Computer>() {
  7. @Override public int compare(Computer lhs, Computer rhs) {
  8. if(lhs.getNode()==Jenkins.this) return -1;
  9. if(rhs.getNode()==Jenkins.this) return 1;
  10. return lhs.getName().compareTo(rhs.getName());
  11. }
  12. });
  13. return r;
  14. }

代码示例来源:origin: peter-lawrey/Java-Chronicle

  1. public void add(String name, Wrapper wrapper) {
  2. wrappers.put(name, wrapper);
  3. wrappersArray = wrappers.values().toArray(new Wrapper[wrappers.size()]);
  4. }

代码示例来源:origin: Bilibili/DanmakuFlameMaster

  1. public void registerFilter(BaseDanmakuFilter filter) {
  2. filters.put(TAG_PRIMARY_CUSTOM_FILTER + "_" + filter.hashCode(), filter);
  3. mFilterArray = filters.values().toArray(mFilterArray);
  4. }

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

  1. new WildcardFileFilter("*"));
  2. final int count = files.size();
  3. final Object[] fileObjs = files.toArray();
  4. assertEquals(fileNames.length, files.size());
  5. for (int j = 0; !found && j < fileNames.length; ++j) {
  6. if (fileNames[j].equals(((File) fileObjs[i]).getName())) {
  7. foundFileNames.put(fileNames[j], fileNames[j]);
  8. found = true;
  9. assertEquals(foundFileNames.size(), fileNames.length);

代码示例来源:origin: lettuce-io/lettuce-core

  1. @SuppressWarnings("unchecked")
  2. private <T> T[] toArray(Collection<T> c) {
  3. Class<T> cls = (Class<T>) c.iterator().next().getClass();
  4. T[] array = (T[]) Array.newInstance(cls, c.size());
  5. return c.toArray(array);
  6. }

代码示例来源:origin: SonarSource/sonarqube

  1. /**
  2. * @since 4.2
  3. */
  4. public Language[] all() {
  5. Collection<Language> languages = map.values();
  6. return languages.toArray(new Language[languages.size()]);
  7. }

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

  1. private static Map<Direction, RelationshipType[]> toTypeMap(
  2. Map<Direction, Collection<RelationshipType>> tempMap )
  3. {
  4. // Remove OUT/IN where there is a BOTH
  5. Collection<RelationshipType> both = tempMap.get( Direction.BOTH );
  6. tempMap.get( Direction.OUTGOING ).removeAll( both );
  7. tempMap.get( Direction.INCOMING ).removeAll( both );
  8. // Convert into a final map
  9. Map<Direction, RelationshipType[]> map = new EnumMap<>( Direction.class );
  10. for ( Map.Entry<Direction, Collection<RelationshipType>> entry : tempMap.entrySet() )
  11. {
  12. if ( !entry.getValue().isEmpty() )
  13. {
  14. map.put( entry.getKey(), entry.getValue().toArray( new RelationshipType[entry.getValue().size()] ) );
  15. }
  16. }
  17. return map;
  18. }

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

  1. static <E> ImmutableMultiset<E> create(Collection<? extends Entry<? extends E>> entries) {
  2. @SuppressWarnings("unchecked")
  3. Entry<E>[] entriesArray = entries.toArray(new Entry[0]);
  4. Map<E, Integer> delegateMap = Maps.newHashMapWithExpectedSize(entriesArray.length);
  5. long size = 0;
  6. for (int i = 0; i < entriesArray.length; i++) {
  7. Entry<E> entry = entriesArray[i];
  8. int count = entry.getCount();
  9. size += count;
  10. E element = checkNotNull(entry.getElement());
  11. delegateMap.put(element, count);
  12. if (!(entry instanceof Multisets.ImmutableEntry)) {
  13. entriesArray[i] = Multisets.immutableEntry(element, count);
  14. }
  15. }
  16. return new JdkBackedImmutableMultiset<>(
  17. delegateMap, ImmutableList.asImmutableList(entriesArray), size);
  18. }

代码示例来源:origin: stackoverflow.com

  1. public static void copyBeanProperties(
  2. final Object source,
  3. final Object target,
  4. final Collection<String> includes){
  5. final Collection<String> excludes = new ArrayList<String>();
  6. final PropertyDescriptor[] propertyDescriptors =
  7. BeanUtils.getPropertyDescriptors(source.getClass());
  8. for(final PropertyDescriptor propertyDescriptor : propertyDescriptors){
  9. String propName = propertyDescriptor.getName();
  10. if(!includes.contains(propName)){
  11. excludes.add(propName);
  12. }
  13. }
  14. BeanUtils.copyProperties(
  15. source, target, excludes.toArray(new String[excludes.size()]));
  16. }

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

  1. /**
  2. * @param c Closure.
  3. */
  4. public static GridResourceField[] toArray(Collection<GridResourceField> c) {
  5. if (c.isEmpty())
  6. return EMPTY_ARRAY;
  7. return c.toArray(new GridResourceField[c.size()]);
  8. }

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

  1. @Override
  2. public DataSource get() {
  3. Map<String, DataSource> dataSourceMap = getDataSourceMap();
  4. if (dataSourceMap == null || dataSourceMap.isEmpty()) {
  5. return null;
  6. }
  7. Collection<DataSource> targetDataSourceSet;
  8. if (blacklist == null || blacklist.isEmpty() || blacklist.size() >= dataSourceMap.size()) {
  9. targetDataSourceSet = dataSourceMap.values();
  10. } else {
  11. targetDataSourceSet = new HashSet<DataSource>(dataSourceMap.values());
  12. for (DataSource b : blacklist) {
  13. targetDataSourceSet.remove(b);
  14. }
  15. }
  16. DataSource[] dataSources = targetDataSourceSet.toArray(new DataSource[] {});
  17. if (dataSources != null && dataSources.length > 0) {
  18. return dataSources[random.nextInt(targetDataSourceSet.size())];
  19. }
  20. return null;
  21. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testGroupByWithElementSelector() {
  3. Observable<String> source = Observable.just("one", "two", "three", "four", "five", "six");
  4. Observable<GroupedObservable<Integer, Integer>> grouped = source.groupBy(length, length);
  5. Map<Integer, Collection<Integer>> map = toMap(grouped);
  6. assertEquals(3, map.size());
  7. assertArrayEquals(Arrays.asList(3, 3, 3).toArray(), map.get(3).toArray());
  8. assertArrayEquals(Arrays.asList(4, 4).toArray(), map.get(4).toArray());
  9. assertArrayEquals(Arrays.asList(5).toArray(), map.get(5).toArray());
  10. }

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

  1. @Override
  2. public Collection<V> create(Object... elements) {
  3. @SuppressWarnings("unchecked")
  4. V[] valuesArray = (V[]) elements;
  5. // Start with a suitably shaped collection of entries
  6. Collection<Entry<K, V>> originalEntries = mapGenerator.getSampleElements(elements.length);
  7. // Create a copy of that, with the desired value for each value
  8. Collection<Entry<K, V>> entries = new ArrayList<>(elements.length);
  9. int i = 0;
  10. for (Entry<K, V> entry : originalEntries) {
  11. entries.add(Helpers.mapEntry(entry.getKey(), valuesArray[i++]));
  12. }
  13. return mapGenerator.create(entries.toArray()).values();
  14. }

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

  1. @Override
  2. public Set<K> create(Object... elements) {
  3. @SuppressWarnings("unchecked")
  4. K[] keysArray = (K[]) elements;
  5. // Start with a suitably shaped collection of entries
  6. Collection<Entry<K, V>> originalEntries = mapGenerator.getSampleElements(elements.length);
  7. // Create a copy of that, with the desired value for each key
  8. Collection<Entry<K, V>> entries = new ArrayList<>(elements.length);
  9. int i = 0;
  10. for (Entry<K, V> entry : originalEntries) {
  11. entries.add(Helpers.mapEntry(keysArray[i++], entry.getValue()));
  12. }
  13. return mapGenerator.create(entries.toArray()).keySet();
  14. }

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

  1. /**
  2. * Parse insert values.
  3. *
  4. * @param insertStatement insert statement
  5. */
  6. public void parse(final InsertStatement insertStatement) {
  7. Collection<Keyword> valueKeywords = new LinkedList<>();
  8. valueKeywords.add(DefaultKeyword.VALUES);
  9. valueKeywords.addAll(Arrays.asList(getSynonymousKeywordsForValues()));
  10. if (lexerEngine.skipIfEqual(valueKeywords.toArray(new Keyword[valueKeywords.size()]))) {
  11. parseValues(insertStatement);
  12. }
  13. }

代码示例来源:origin: ben-manes/caffeine

  1. /**
  2. * Returns a future that waits for all of the dependent futures to complete and returns the
  3. * combined mapping if successful. If any future fails then it is automatically removed from
  4. * the cache if still present.
  5. */
  6. private CompletableFuture<Map<K, V>> composeResult(Map<K, CompletableFuture<V>> futures) {
  7. if (futures.isEmpty()) {
  8. return CompletableFuture.completedFuture(Collections.emptyMap());
  9. }
  10. @SuppressWarnings("rawtypes")
  11. CompletableFuture<?>[] array = futures.values().toArray(new CompletableFuture[0]);
  12. return CompletableFuture.allOf(array).thenApply(ignored -> {
  13. Map<K, V> result = new LinkedHashMap<>(futures.size());
  14. futures.forEach((key, future) -> {
  15. V value = future.getNow(null);
  16. if (value != null) {
  17. result.put(key, value);
  18. }
  19. });
  20. return Collections.unmodifiableMap(result);
  21. });
  22. }

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

  1. private Annotation[] resolveAnnotations() {
  2. Annotation[] annotations = annotationCache.get(this);
  3. if (annotations == null) {
  4. Map<Class<? extends Annotation>, Annotation> annotationMap = new LinkedHashMap<>();
  5. addAnnotationsToMap(annotationMap, getReadMethod());
  6. addAnnotationsToMap(annotationMap, getWriteMethod());
  7. addAnnotationsToMap(annotationMap, getField());
  8. annotations = annotationMap.values().toArray(new Annotation[0]);
  9. annotationCache.put(this, annotations);
  10. }
  11. return annotations;
  12. }

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

  1. someMap.put(new TypedStringValue("key${age}"), new TypedStringValue("${age}"));
  2. someMap.put(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}"));
  3. someMap.put("key1", new RuntimeBeanReference("${ref}"));
  4. someMap.put("key2", "${age}name");
  5. MutablePropertyValues innerPvs = new MutablePropertyValues();
  6. assertEquals("myvarname98", tb2.getName());
  7. assertEquals(tb2, tb1.getSpouse());
  8. assertEquals(1, tb1.getSomeMap().size());
  9. assertEquals("myValue", tb1.getSomeMap().get("myKey"));
  10. assertEquals(2, tb2.getStringArray().length);
  11. assertEquals(System.getProperty("os.name"), tb2.getStringArray()[0]);
  12. assertEquals("98", tb2.getStringArray()[1]);
  13. assertEquals(2, tb2.getFriends().size());
  14. assertEquals("na98me", tb2.getFriends().iterator().next());
  15. assertEquals(tb2, tb2.getFriends().toArray()[1]);
  16. assertEquals(3, tb2.getSomeSet().size());
  17. assertTrue(tb2.getSomeSet().contains("na98me"));
  18. assertTrue(tb2.getSomeSet().contains(tb2));
  19. assertTrue(tb2.getSomeSet().contains(new Integer(98)));
  20. assertEquals(6, tb2.getSomeMap().size());
  21. assertEquals("98", tb2.getSomeMap().get("key98"));
  22. assertEquals(tb2, tb2.getSomeMap().get("key98ref"));

相关文章