In this article, we will discuss Collections class – a utility class for Collection framework which has useful methods for below operations like,
- 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
1. 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
2. Collections method:
Collections method | 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 |
3. 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
Related Articles:
- Collections class – utility class for Collection
- Sorting ArrayList using Comparable and Comparator
- Searching element from ArrayList using Binary Search Algorithm
- How to Reverse order of elements in ArrayList
- How to Reverse order of Comparator
- How to count duplicate elements of ArrayList
- How to swap elements of ArrayList
- How to copy elements of one ArrayList to another List
- How to shuffle elements of ArrayList
- How to make an ArrayList read-only ?
- How to make a HashSet read-only ?
- How to make a HashMap read-only ?
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 !!