A data structure is a collection of data organized in some fashion. The structure not only stores data but also supports operations for accessing and manipulating the data. The java.util package contains one of Java’s most powerful subsystems: The Collections Framework. The Collections Framework is a sophisticated hierarchy of interfaces and classes that provide state-of-the-art technology for managing groups of objects.
You can perform following activity using Java collection framework,
Key Interfaces and classes of collection framework
There are some other classes in collection framework which do not extend Collection Interface they implement Map interface.
We can say collection has in 4 basic flavors as below,
We can have sub-flavors of collection classes like sorted, unsorted, ordered and unordered.
Ordered: When a collection is ordered, it means you can iterate through the collection in a specific (not random) order
Sorted: A sorted collection means that the order of objects in the collection is determined according to some rule or rules, known as the sorting order. A sort order has nothing to do with when an object was added to the collection, or when was the last time it was accessed, or what "position" it was added at.
Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements.
An Iterator is an object that's associated with a specific collection. It let's you loop through the collection step by step. There are two important Iterator methods.
We will see an example of the use of Iterator Interface in LinkedHashSet and HashMap Tutorial.
The Comparator interface gives you the capability to sort a given collection any number of different ways. The another handy thing about the Comparator interface is that you can use it to sort instances of any class—even classes you can't modify—unlike the Comparable interface, which forces you to change the class whose instances you want to sort.
The Comparator interface is also very easy to implement, having only one method, compare().The Comparator.compare() method returns an int.
int compare(objOne, objTwo)
Compare() method returns
We will see Java program using this method in TreeMap/TreeSet Tutorial.
Below table summarizes the discussion on collection framework.
|Collection Class name||Ordered||Sorted|
|TreeMap||Sorted||By natural order or custom order|
|LinkedHashMap||By insertion order or last access order||No|
|TreeSet||Sorted||By natural order or custom order|
|LinkedHashSet||By insertion order||No|
|Priority queue||Sorted||By to-do order|