A small confusion comes to a novice at this juncture between collection and collections. Both are very different. Collection is an interface and Collections is a class, both from java.util package. Collection is the root interface from which almost all data structures are derived, commonly known as collection framework. Collections class contains many static methods with which data structures manipulation becomes easier. I can say only word: Collections class is a boon (gift by the designers) to data structures which you too accept when you go through all the example codes given here under. I say boon because the lots of trouble you encountered with laborious coding of loops in C/C++ to manipulate DS elements is overcome with a simple method call. This is the best place to prove Java is simple language to practice.
There comes another class java.util.Arrays which is meant for array elements manipulation.
All the static methods of Collections class are put together known as algorithms. These convenient static methods either operate on data structure elements or return data structures. We know earlier, data structures are subclasses of Collection interface and known as collection framework.
Following is the class signature
Following are the most frequently used Java Collections API Methods (known as algorithms) and are discussed elaborately later with examples. At the end, links are given for programs to use these methods.
- static void sort(List list1): Sorts the list list1 into ascending order according to the natural sequence (a before b or 1 before 2) of the elements.
- static void sort(List list1, Comparator comp): Sorts the list list1 as per the order specified by the comparator object comp and not default natural order.
- static int binarySearch(List list1, Object obj1): Searches the obj1 in the list list1. Returns the index number of the element obj1. Before applying this method, the elements must be sorted earlier with sort() method; else unexpected results will be obtained at different times of execution.
- static int binarySearch(List list1, Object obj1, Comparator comp): Searches the element obj1 in the list list1. Returns the index number of obj1. Before applying this method, the elements must be sorted earlier with sort(List, Comparator) method; else unexpected results will be obtained at different times of execution
- static void reverse(List list1): Existing order of the elements in the list list1 are reversed. Again reversing gets the original order.
- static void shuffle(List list1): Shuffles the existing elements of list1 randomly. For repeated execution of the method, elements with different order are obtained.
- static void swap(List list1, int index1, int index2): List list1 elements at index numbers index1 and index2 are swapped.
- static void fill(List list1, Object obj1): Replaces all the elements of list1 with obj1. Earlier elements are lost. This method is used to fill all the elements with the same values.
- static void copy(List destination1, List source1): Copies all the elements of List source1 into the destination1 list. It is like arraycopy() method.
- static Object min(Collection col1): Returns the element with the minimum value in the collection col1.
- static Object max(Collection col1): Returns the element with the maximum value in the collection col1.
- static void rotate(List list1, int dist1): Rotates the elements in the list list1 by the specified distance dist1.
- static boolean replaceAll(List list1, Object oldObj, Object newObj): Replaces the old element oldObj with the new element newObj in the list list1 (all the occurrences). Returns true when the operation is successful (when the oldObj exists).
- static int indexOfSubList(List source, List target): Checks for the existence of a few elements in another list. Returns the index number of the first occurrence of the target list in the source list. It is like indexOf() of String class. If no elements are found, returns -1. Added to Collections class with JDK 1.4.
- static int lastIndexOfSubList(List source, List target): Checks for the existence of a few elements in another list. Returns the index number of the last occurrence of the target list in the source list. It is used when the same element exists many times. It is like lastIndexOf() of String class. If no elements are found, returns -1. Added to Collections class with JDK 1.4.
- static Collection unmodifiableCollection(Collection col1): Returns an unmodifiable (read-only) version of col1. Any methods like add() or remove(), if applied throws UnsupportedOperationException. To use with map, list and set, also exists the methods like unmodifiableMap(), unmodifiableSortedMap(), unmodifiableSet(), unmodifiableSortedSet(), unmodifiableList() etc. exist.
- static Collection synchronizedCollection(Collection col1): Returns a synchronized version of collection col1. It is used for thread-safe operations. The synchronized version can be used safely in a multithreaded environment. Methods like synchronizedSet(), synchronizedSortedSet(), synchronizedList(), synchronizedMap() and synchronizedSortedMap() also exist to use with set, list and map.
- static Collection checkedCollection(Collection col1, Class obj1): Returns a generics version of collection col1 that can store only elements of type obj1. But still col1 is not modified and non-generics. checkedSet(), checkedSortedSet(), checkedList() and checkedMap() methods also exist to get a checked version of set, list and map classes. Introduced with JDK 1.5.
- static List nCopies(int n1, Object obj1): Returns an immutable list containing n1 elements of the object obj1.
- static Enumeration enumeration(Collection col1): Returns an enumeration object of Collections col1. With this method, the collections class elements can be iterated with Enumeration. It is a good example for the interoperability between collection classes and legacy classes.
- static ArrayList list(Enumeration e): With this method, the Enumeration object e containing elements can be converted into an ArrayList. That is, vector elements can be converted into ArrayList through Enumeration; again interoperability between collection classes and legacy classes.
- static int frequency(Collection col1, Object obj1): Checks how many times obj1 exists in collection col1, returns as an integer value.
- static boolean disjoint(Collection col1, Collection col2): Returns true if the collection classes col1 and col2 do not have any common elements. Introduced with JDK 1.5.
- static boolean addAll(Collection col1, Object obj1): Here, obj1 can be a single element or an array. Adds obj1 to the collection col1.
- List singletonList(Object obj1): Returns an immutable list containing only one element obj1. The element obj1 cannot be removed or added no elements. Introduced with JDK 1.3.
Note: The static methods of Collections class throws NullPointerException when the collection object provided is null.
Note: If a collection class does not support any operation (method like adding an element to a list returned by singletonList() method), the method throws UnsupportedOperationException.
Programs on the above Methods