java.util.Collections类的使用及代码示例

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

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

Collections介绍

[英]This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends.

The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.

The documentation for the polymorphic algorithms contained in this class generally includes a brief description of the implementation. Such descriptions should be regarded as implementation notes, rather than parts of the specification. Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used by sort does not have to be a mergesort, but it does have to be stable.)

The "destructive" algorithms contained in this class, that is, the algorithms that modify the collection on which they operate, are specified to throw UnsupportedOperationException if the collection does not support the appropriate mutation primitive(s), such as the set method. These algorithms may, but are not required to, throw this exception if an invocation would have no effect on the collection. For example, invoking the sort method on an unmodifiable list that is already sorted may or may not throw UnsupportedOperationException.

This class is a member of the Java Collections Framework.
[中]此类仅由对集合进行操作或返回集合的静态方法组成。它包含对集合“包装器”(wrappers)进行操作的多态算法,包装器返回由指定集合支持的新集合,以及一些其他零碎的东西。
如果提供给此类的集合或类对象为null,则此类的所有方法都会引发NullPointerException。
此类中包含的多态算法文档通常包括实现的简要描述。此类描述应视为实施说明,而不是规范的一部分。只要遵守规范本身,实现者应该可以随意替换其他算法。(例如,sort使用的算法不必是mergesort,但必须是稳定的。)
此类中包含的“破坏性”算法,即修改其操作的集合的算法,被指定为在集合不支持适当的变异原语(如set方法)时引发UnsupportedOperationException。如果调用对集合没有影响,则这些算法可能(但不是必需)引发此异常。例如,对已排序的不可修改列表调用sort方法可能会或可能不会引发UnsupportedOperationException。
此类是Java Collections Framework的成员。

代码示例

canonical example by Tabnine

private void mappingWordsLength(List<String> wordsList) {
 Map<Integer, Set<String>> mapping = new HashMap<>();
 for (String word : wordsList) {
  mapping.computeIfAbsent(word.length(), HashSet::new).add(word);
 }
 List<Integer> lengths = new LinkedList<>(mapping.keySet());
 Collections.sort(lengths);
 lengths.forEach(n -> System.out.println(mapping.get(n).size() + " words with " + n + " chars"));
}

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

@Override
  public Iterable<Integer> apply(Integer v) throws Exception {
    return Collections.<Integer>emptyList();
  }
})

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

@Override
  public List<T> apply(List<T> v) {
    Collections.sort(v, comparator);
    return v;
  }
}

代码示例来源:origin: square/retrofit

@Override List<? extends Converter.Factory> defaultConverterFactories() {
 return Build.VERSION.SDK_INT >= 24
   ? singletonList(OptionalConverterFactory.INSTANCE)
   : Collections.<Converter.Factory>emptyList();
}

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

Collection<V> unmodifiableEmptyCollection() {
 // These return false, rather than throwing a UOE, on remove calls.
 return (unfiltered instanceof SetMultimap)
   ? Collections.<V>emptySet()
   : Collections.<V>emptyList();
}

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

private static <K, V> Map<K, V> unmodifiableMap(Map<K, ? extends V> map) {
 if (map instanceof SortedMap) {
  return Collections.unmodifiableSortedMap((SortedMap<K, ? extends V>) map);
 } else {
  return Collections.unmodifiableMap(map);
 }
}

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

private static Iterable<File> fileTreeChildren(File file) {
 // check isDirectory() just because it may be faster than listFiles() on a non-directory
 if (file.isDirectory()) {
  File[] files = file.listFiles();
  if (files != null) {
   return Collections.unmodifiableList(Arrays.asList(files));
  }
 }
 return Collections.emptyList();
}

代码示例来源:origin: square/retrofit

List<? extends CallAdapter.Factory> defaultCallAdapterFactories(
  @Nullable Executor callbackExecutor) {
 if (callbackExecutor != null) {
  return singletonList(new ExecutorCallAdapterFactory(callbackExecutor));
 }
 return singletonList(DefaultCallAdapterFactory.INSTANCE);
}

代码示例来源:origin: square/retrofit

/** Trusted constructor assumes ownership of {@code arguments}. */
Invocation(Method method, List<?> arguments) {
 this.method = method;
 this.arguments = Collections.unmodifiableList(arguments);
}

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

@Override
  public Iterable<Integer> apply(Integer v) {
    return (v % 2) == 0 ? Collections.singleton(1) : Collections.<Integer>emptySet();
  }
})

代码示例来源:origin: square/okhttp

Builder(Request request) {
 this.url = request.url;
 this.method = request.method;
 this.body = request.body;
 this.tags = request.tags.isEmpty()
   ? Collections.emptyMap()
   : new LinkedHashMap<>(request.tags);
 this.headers = request.headers.newBuilder();
}

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

@Override
 public Map<Object, Object> apply(Map<Object, Object> input) {
  return Collections.unmodifiableMap(input);
 }
};

代码示例来源:origin: square/okhttp

/** Returns an immutable copy of {@code map}. */
public static <K, V> Map<K, V> immutableMap(Map<K, V> map) {
 return map.isEmpty()
   ? Collections.emptyMap()
   : Collections.unmodifiableMap(new LinkedHashMap<>(map));
}

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

public void testPartition_singleton1() {
 Iterable<Integer> source = Collections.singleton(1);
 Iterable<List<Integer>> partitions = Iterables.partition(source, 1);
 assertEquals(1, Iterables.size(partitions));
 assertEquals(Collections.singletonList(1), partitions.iterator().next());
}

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

@Override
 public List<String> order(List<String> insertionOrder) {
  Collections.sort(insertionOrder, Collections.reverseOrder());
  return insertionOrder;
 }
}

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

public MethodNotAllowedException(String method, @Nullable Collection<HttpMethod> supportedMethods) {
  super(HttpStatus.METHOD_NOT_ALLOWED, "Request method '" + method + "' not supported");
  Assert.notNull(method, "'method' is required");
  if (supportedMethods == null) {
    supportedMethods = Collections.emptySet();
  }
  this.method = method;
  this.supportedMethods = Collections.unmodifiableSet(new HashSet<>(supportedMethods));
}

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

@Override
 public List<String> create(String[] elements) {
  List<String> innerList = new ArrayList<>();
  Collections.addAll(innerList, elements);
  return Collections.unmodifiableList(innerList);
 }
})

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

@Override
  public Iterable<Integer> apply(Object v) throws Exception {
    return Collections.singleton(1);
  }
}));

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

@Override
public Set<K> keySet() {
 Set<K> result = keySet;
 if (result == null) {
  keySet = result = Collections.unmodifiableSet(delegate.keySet());
 }
 return result;
}

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

@Override
 public Set<Feature<? super Void>> getImpliedFeatures() {
  return Collections.emptySet();
 }
}

相关文章

微信公众号

最新文章

更多

Collections类方法