The Collection Algorithms in java

The Collections Framework defines several algorithms that can be applied to collections and maps. These algorithms are defined as static methods within the Collections class

The Algorithms Defined by Collections

 Method Description
 static <T> booleanInserts the elements specified by elements into the
 addAll(Collection <? super T> c,collection specified by c. Returns true if the
 T … elements)elements were added and false otherwise.
   
 static <T> Queue<T> asLifoQueue(Deque<T> c)Returns a last-in, first-out view of c. (Added by Java
   SE 6.)
    
 static <T> Searches for value in list ordered according to c.
 int binarySearch(List<? extends T> list,Returns the position of value in list, or a negative
  T value,value if value is not found.
  Comparator<? super T> c) 
    
 static <T> Searches for value in list. The list must be sorted.
 int binarySearch(List<? extendsReturns the position of value in list, or a negative
  Comparable<? super T>> list,value if value is not found.
  T value) 
   
 static <E> Collection<E>Returns a run-time type-safe view of a collection.
 checkedCollection(Collection<E> c,An attempt to insert an incompatible element will
  Class<E> t)cause a ClassCastException.
   
 static <E> List<E>Returns a run-time type-safe view of a List. An
 checkedList(List<E> c, Class<E> t)attempt to insert an incompatible element will
   cause a ClassCastException.
   
 static <K, V> Map<K, V>Returns a run-time type-safe view of a Map. An
 checkedMap(Map<K, V> c,attempt to insert an incompatible element will
  Class<K> keyT,cause a ClassCastException.
  Class<V> valueT) 
   
 static <E> List<E>Returns a run-time type-safe view of a Set. An
 checkedSet(Set<E> c, Class<E> t)attempt to insert an incompatible element will
   cause a ClassCastException.
   
 static <K, V> SortedMap<K, V>Returns a run-time type-safe view of a SortedMap.
 checkedSortedMap(SortedMap<K, V> c,An attempt to insert an incompatible element will
  Class<K> keyT,cause a ClassCastException.
  Class<V> valueT) 
   
 static <E> SortedSet<E>Returns a run-time type-safe view of a SortedSet.
 checkedSortedSet(SortedSet<E> c, Class<E> t)An attempt to insert an incompatible element will
   cause a ClassCastException.
   
 static <T> void copy(List<? super T> list1,Copies the elements of list2 to list1.
  List<? extends T> list2) 
   
 static boolean disjoint(Collection<?> a,Compares the elements in a to elements in b.
  Collection<?> b)Returns true if the two collections contain no
   common elements (i.e., the collections contain
   disjoint sets of elements). Otherwise, returns true.
   
 static <T> List<T> emptyList( )Returns an immutable, empty List object of the
   inferred type.
   
 static <K, V> Map<K, V> emptyMap( )Returns an immutable, empty Map object of the
   inferred type.
   
 static <T> Set<T> emptySet( )Returns an immutable, empty Set object of the
   inferred type.
   
 static <T> Enumeration<T>Returns an enumeration over c. (See “The
 enumeration(Collection<T> c)Enumeration Interface,” later in this chapter.)
   
 static <T> void fill(List<? super T> list, T obj)Assigns obj to each element of list.
The Algorithms Defined by Collections
Method Description 
static int frequency(Collection<?> c, Object obj)Counts the number of occurrences of obj in c and 
  returns the result. 
    
static int indexOfSubList(List<?> list, Searches list for the first occurrence of subList. 
List<?> subList) Returns the index of the first match, or –1 if no 
  match is found. 
    
static int lastIndexOfSubList(List<?> list, Searches list for the last occurrence of subList. 
List<?> subList)Returns the index of the last match, or –1 if no 
  match is found. 
static <T> Returns an ArrayList that contains the elements 
ArrayList<T> list(Enumeration<T> enum)of enum. 
static <T> T max(Collection<? extends T> c,Returns the maximum element in c as determined 
Comparator<? super T> comp)by comp. 
static <T extends Object & Returns the maximum element in c as determined 
Comparable<? super T>> by natural ordering. The collection need not be 
T max(Collection<? extends T> c) sorted. 
   
static <T> T min(Collection<? extends T> c,Returns the minimum element in c as determined 
Comparator<? super T> comp)by comp. The collection need not be sorted. 
    
static <T extends Object & Returns the minimum element in c as determined 
Comparable<? superT>> by natural ordering. 
T min(Collection<? extends T> c)   
    
static <T> List<T> nCopies(int num, T obj) Returns num copies of obj contained in an immutable 
  list. num must be greater than or equal to zero. 
   
static <E> Set<E> newSetFromMap(Map<E, Boolean> m)Creates and returns a set backed by the map 
  specified by m, which must be empty at the time 
  this method is called. (Added by Java SE 6.) 
    
static <T> boolean replaceAll(List<T> list, Replaces all occurrences of old with new in list. 
T old, T new)Returns true if at least one replacement occurred. 
  Returns false, otherwise. 
    
static void reverse(List<T> list) Reverses the sequence in list. 
    
static <T> Comparator<T> Returns a reverse comparator based on the one 
reverseOrder(Comparator<T> comp) passed in comp. That is, the returned comparator 
  reverses the outcome of a comparison that uses 
  comp. 
    
static <T> Comparator<T> reverseOrder( ) Returns a reverse comparator, which is a 
  comparator that reverses the outcome of a 
  comparison between two elements. 
    
static void rotate(List<T> list, int n) Rotates list by n places to the right. To rotate left, 
  use a negative value for n. 
    
static void shuffle(List<T> list, Random r) Shuffles (i.e., randomizes) the elements in list by 
  using r as a source of random numbers. 
    
static void shuffle(List<T> list) Shuffles (i.e., randomizes) the elements in list. 
    
static <T> Set<T> singleton(T obj) Returns obj as an immutable set. This is an easy 
  way to convert a single object into a set. 
    
static <T> List<T> singletonList(T obj) Returns obj as an immutable list. This is an easy 
  way to convert a single object into a list. 
    
static <K, V> Map<K, V> Returns the key/value pair k/v as an immutable 
singletonMap(K k, V v) map. This is an easy way to convert a single key/ 
  value pair into a map. 
The Algorithms Defined by Collections
 Method Description
 static <T> Sorts the elements of list as determined by comp.
 void sort(List<T> list, 
  Comparator<? super T> comp) 
   
 static <T extends Comparable<? super T>>Sorts the elements of list as determined by their
 void sort(List<T> list)natural ordering.
   
 static void swap(List<?> list,Exchanges the elements in list at the indices
  int idx1, int idx2)specified by idx1 and idx2.
   
 static <T> Collection<T>Returns a thread-safe collection backed by c.
 synchronizedCollection(Collection<T> c) 
   
 static <T> List<T> synchronizedList(List<T> list)Returns a thread-safe list backed by list.
   
 static <K, V> Map<K, V>Returns a thread-safe map backed by m.
 synchronizedMap(Map<K, V> m) 
   
 static <T> Set<T> synchronizedSet(Set<T> s)Returns a thread-safe set backed by s.
   
 static <K, V> SortedMap<K, V>Returns a thread-safe sorted map backed by sm.
 synchronizedSortedMap(SortedMap<K, V> sm) 
   
 static <T> SortedSet<T>Returns a thread-safe set backed by ss.
 synchronizedSortedSet(SortedSet<T> ss) 
   
 static <T> Collection<T>Returns an unmodifiable collection backed by c.
 unmodifiableCollection( 
 Collection<? extends T> c) 
   
 static <T> List<T>Returns an unmodifiable list backed by list.
 unmodifiableList(List<? extends T> list) 
   
 static <K, V> Map<K, V>Returns an unmodifiable map backed by m.
 unmodifiableMap(Map<? extends K, 
  ? extends V> m) 
   
 static <T> Set<T>Returns an unmodifiable set backed by s.
 unmodifiableSet(Set<? extends T> s) 
   
 static <K, V> SortedMap<K, V>Returns an unmodifiable sorted map backed
 unmodifiableSortedMap(SortedMap<K,by sm.
  ? extends V> sm) 
 static <T> SortedSet<T>Returns an unmodifiable sorted set backed by ss.
 unmodifiableSortedSet(SortedSet<T> ss) 
The Algorithms Defined by Collections

Leave a Comment