Tabnine Logo
Collections
Code IndexAdd Tabnine to your IDE (free)

How to use
Collections
in
java.util

Best Java code snippets using java.util.Collections (Showing top 20 results out of 282,249)

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();
 }
}
java.utilCollections

Javadoc

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.

Most used methods

  • emptyList
    Returns the empty list (immutable). This list is serializable.This example illustrates the type-safe
  • sort
  • singletonList
    Returns an immutable list containing only the specified object. The returned list is serializable.
  • unmodifiableList
    Returns an unmodifiable view of the specified list. This method allows modules to provide users with
  • emptyMap
    Returns the empty map (immutable). This map is serializable.This example illustrates the type-safe w
  • emptySet
    Returns the empty set (immutable). This set is serializable. Unlike the like-named field, this metho
  • unmodifiableMap
    Returns an unmodifiable view of the specified map. This method allows modules to provide users with
  • singleton
    Returns an immutable set containing only the specified object. The returned set is serializable.
  • unmodifiableSet
    Returns an unmodifiable view of the specified set. This method allows modules to provide users with
  • singletonMap
    Returns an immutable map, mapping only the specified key to the specified value. The returned map is
  • addAll
    Adds all of the specified elements to the specified collection. Elements to be added may be specifie
  • reverse
    Reverses the order of the elements in the specified list. This method runs in linear time.
  • addAll,
  • reverse,
  • unmodifiableCollection,
  • shuffle,
  • enumeration,
  • list,
  • synchronizedMap,
  • synchronizedList,
  • reverseOrder,
  • emptyIterator

Popular in Java

  • Running tasks concurrently on multiple threads
  • notifyDataSetChanged (ArrayAdapter)
  • scheduleAtFixedRate (ScheduledExecutorService)
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • Random (java.util)
    This class provides methods that return pseudo-random values.It is dangerous to seed Random with the
  • Stack (java.util)
    Stack is a Last-In/First-Out(LIFO) data structure which represents a stack of objects. It enables u
  • BlockingQueue (java.util.concurrent)
    A java.util.Queue that additionally supports operations that wait for the queue to become non-empty
  • Reference (javax.naming)
  • Get (org.apache.hadoop.hbase.client)
    Used to perform Get operations on a single row. To get everything for a row, instantiate a Get objec
  • Runner (org.openjdk.jmh.runner)
  • CodeWhisperer alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now