Collections Utility class
The java.util.Collections
class is an utility class, an utility class defines a set of static
methods that perform common operations, all the methods of
Collections are public and static.
The java API says about the Collection
class “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 Collections class has methods for
changing the order of lists:
void reverse(List<?> list):
Reverses the order of the elements
void rotate(List<?> list, int
distance): Rotates the elements in the specified list by the
specified distance.
void shuffle(List<?> list):
Randomly permutes the list elements
void shuffle(List<?> list,
Random rnd): Randomly permutes the list using the randomness
source rnd
<T extends Comparable<? super T>>
void sort(List<T> list): Sorts the supplied list using
natural ordering
<T> void sort(List<T>
list, Comparator<? super T> c): Sorts the supplied list
using the supplied ordering
void swap(List<?> list, int i,
int j): Swaps the elements at the specified positions
It has methods for changing the
contents of a list, the copy() transfers elements from the source
list into an initial a the destination list, the fill() replaces
every element of a list with a specified object and replaceAll()
replaces every occurrence of one value in a list with another.
<T> void copy(List<? super
T> dest, List<? extends T> src): Copies all of the
elements from one list into another
<T> void fill(List<? super
T> list, T obj): Replaces every element of list with obj
<T> boolean replaceAll(List<T>
list, T old, T new): Replaces all occurrences of old in list with
new.
It has methods for finding elements in
ordered collections:
<T extends Object & Comparable<?
super T>> T max(Collection<? extends T> coll):
Returns the maximum element using natural ordering.
<T> T max(Collection<?
extends T> coll, Comparator<? super T> comp): Returns
the maximum element using the supplied comparator
<T extends Object & Comparable<?
super T>> T min(Collection<? extends T> coll):
Returns the minimum element using natural ordering
<T> T min(Collection<?
extends T> coll, Comparator<? super T> comp): Returns
the minimum element using the supplied comparator
<T> int binarySearch(List<?
extends Comparable<? super T>> list, T key): Searches
for key using binary search
<T> int binarySearch(List<?
extends T> list, T key, Comparator<? super T> c):
Searches for key using binary search
int indexOfSubList(List<?>
source, List<?> target): Finds the first sublist of source
which matches target
int lastIndexOfSubList(List<?>
source, List<?> target): Finds the last sublist of source
which matches target
Some methods require that the
collection should be ordered before using them otherwise their
outcome will make no sense, this is the case of the binarySearch(),
if the list is not ordered then the binarySearch() will return a
wrong value, Example:
In the example you can see how the
collection is ordered before using the binarySearch() method.
Some methods are overloaded with a
version that takes a Comparator, in this case the type of the
Collection doesn't have to implement the Comparable interface,
Example:
In the example you can see that the
List is of type Person and it doesn't implements the Comparable
interface but at the time to order the collection and search it, a
Comparator instance is used it.
The Collections class has methods for
creating collections:
<T> List<T> emptyList():
Returns the empty list (immutable)
<K,V> Map<K,V> emptyMap():
Returns the empty map (immutable)
<T> Set<T> emptySet():
Returns the empty set (immutable)
These methods can be used to indicate
there are no values, the collections returned by this methods are
immutable, this means no elements can be added to them.
Example:
The previous example will throw a
java.lang.UnsupportedOperationException at the time the new element
was added.
There are also methods for creating
collections containing only a single element.
<T> Set<T> singleton(T
o): Returns an immutable set containing only the specified
object.
<T> List<T> singletonList(T
o): Returns an immutable list containing only the specified
object.
<K,V> Map<K,V>
singletonMap(K key, V value): Returns an immutable map,
mapping only the key K to the value V
The collections returned by this
methods are also immutable and no elements can be added to them.
Example:
The previous example will throw a
java.lang.UnsupportedOperationException at the time the new element
was added.
There is a method that creates a list
containing a number of copies of a given object.
<T> List<T> nCopies(int
n, T o): Returns an immutable list containing n references to the
object o
Sometimes a collection implementation
is not synchronized, in order to make a collection thread safe the
Collections class provides methods for this purpose:
<T> Collection<T>
synchronizedCollection(Collection<T> c);
<T> Set<T>
synchronizedSet(Set<T> s);
<T> List<T>
synchronizedList(List<T> list);
<K, V> Map<K, V>
synchronizedMap(Map<K, V> m);
<T> SortedSet<T>
synchronizedSortedSet(SortedSet<T> s);
<K, V> SortedMap<K, V>
synchronizedSortedMap(SortedMap<K, V> m);
Basically this last post is a summary
of the java.util.Collections class, but if I was doing a tutorial
about collections I had to add it. So with this I finish the tutorial
and I hope you can find a quick reference to the collections API and
learn something new.
http://www.javaproficiency.com/2015/05/java-collections-framework-tutorials.html
ReplyDeleteThanks for sharing nice article .There are good Java Collection Tutorials with example
ReplyDelete