Collections – utility class for Collection

In this article, we will discuss Collections class – a utility class for Collection framework which has useful methods for

  • Sorting
  • Searching
  • Reverse the order of elements
  • Shuffling
  • Swapping
  • Checking frequency of duplicate values of List
  • Returning synchronized version of List, Set or Map
  • etc

 

Collections class:

  • All utility method inside Collections class are static
  • Methods can be invoked directly using class-name, without creating instance
  • This is mostly used for List; because there are alternative for Set and Map

 

Collections methods:

 Collections methods Description
void sort(List list); to sort based on natural ordering (i.e.; ascending order)
void sort(List list, Comparator c); to sort based on specified comparator (i.e.; customized sorting order)
int binarySearch(List list, Object o); to search specified object from the specified list using binary search algorithm

pre-condition: must be sorted before search according to natural ordering

int binarySearch(List list, Object o, Comparator c); to search specified object from the specified list using binary search algorithm

pre-condition: must be sorted according to specified comparator before search

void reverse(List list); to reverse the order of elements in the specified list
Comparator reverseOrder(Comparator c); reverse ordering of specified comparator
int frequency(Collection c, Object o); returns  number of specified object from specified collection
List synchronizedList(List l); returns synchronized version of specified list (i.e.; thread-safe list)
Set synchronizedSet(Set s); returns synchronized version of specified set (i.e.; thread-safe set)
Map synchronizedMap(Map m); returns synchronized version of specified map (i.e.; thread-safe map)
Collection synchronizedCollection(Collection c); returns synchronized version of specified collection (i.e.; thread-safe collection)
void swap(List list, int i, int j); swaps the elements at the specified positions in the specified list
void copy(List dest, List src); Copies from source list to destination list
void shuffle(List list); randomly permutes the specified list using a default source of randomness
void shuffle(List list, Random rnd); randomly permute the specified list using the specified source of randomness

 

 

Factors to consider while discussing any collection class

We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,

  • Underlying data structure
  • Duplicates are allowed or Not
  • Insertion order is maintained or Not
  • Whether NULL insertion is possible or Not
  • If possible, how many NULL values can be inserted
  • Whether collection class provide sorting, by default
  • Is there any way to apply customized sorting
  • Performance, while dealing with retrieval or manipulation (addition/deletion)
  • By default, all methods are synchronized or Not

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
https://docs.oracle.com/javase/7/docs/api/java/util/List.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

 

Happy Coding !!
Happy Learning !!