Java Collections Framework

Java Collections Framework

Collections Framework in Java

The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures.

A collection sometimes called a container is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data.

A collection is the same as the intuitive, mathematical concept of a set. A set is just a group of unique items, meaning that the group contains no duplicates. Java Collections Framework provides a set of interfaces and classes for storing and manipulating groups of data as a single unit, a collection.

The framework provides a convenient API to many of the abstract data types maps, sets, lists, trees, arrays, hashtables, and other collections. Because of their object-oriented design, the Java classes in the Collections Framework encapsulate both the data structures and the algorithms associated with these abstractions. With the Java Collections Framework the programmer easily define higher level data abstractions, such as stacks, queues, and thread safe collections.

You should be able to understand the Collections Framework more easily. The Collections Framework is made up of a set of interfaces for working with groups of objects. The different interfaces describe the different types of groups. For the most part, once you understand the interfaces, you understand the framework. While you always need to create specific implementations of the interfaces, access to the actual collection should be restricted to the use of the interface methods, thus allowing you to change the underlying data structure, without altering the rest of your code. The following diagrams shows the framework interface hierarchy.

What Is a Collections Framework ?

A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following:

  • Interfaces : These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.
  • Implementations : These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.
  • Algorithms : These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality.
Java Collections Framework

Benefits of Java Collections Framework

The Java Collections Framework provides the following benefits :

  1. Reduces programming effort : By providing useful data structures and algorithms, the Collections Framework frees you to concentrate on the important parts of your program rather than on the low-level "plumbing" required to make it work. By facilitating interoperability among unrelated APIs, the Java Collections Framework frees you from writing adapter objects or conversion code to connect APIs.
  2. Increases program speed and quality : The Collections Framework provides high-performance, high-quality implementations of useful data structures and algorithms. The various implementations of each interface are interchangeable, so programs can be easily tuned by switching collection implementations. Because you're freed from the drudgery of writing your own data structures, you'll have more time to devote to improving programs' quality and performance.
  3. Allows interoperability among unrelated APIs : The collection interfaces are the vernacular by which APIs pass collections back and forth. If my network administration API furnishes a collection of node names and if your GUI toolkit expects a collection of column headings, APIs will interoperate seamlessly, even though they were written independently.
  4. Reduces effort to learn and to use new APIs : Many APIs naturally take collections on input and furnish them as output. In the past, each such API had a small sub-API devoted to manipulating its collections. There was little consistency among these ad hoc collections sub-APIs, so you had to learn each one from scratch, and it was easy to make mistakes when using them. With the advent of standard collection interfaces, the problem went away.
  5. Reduces effort to design new APIs : This is the flip side of the previous advantage. Designers and implementers don't have to reinvent the wheel each time they create an API that relies on collections; instead, they can use standard collection interfaces.
  6. Fosters software reuse : New data structures that conform to the standard collection interfaces are by nature reusable. The same goes for new algorithms that operate on objects that implement these interfaces.

What are the Java Collections Framework Classes and Interfaces ?

Java Collections Framework Classes
Sl.No. Collections Framework Class Description
1 AbstractCollection The AbstractCollection class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface.
2 AbstractList The AbstractList class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "random access" data store (such as an array).
3 AbstractMap The AbstractMap class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.
4 AbstractQueue The AbstractQueue class provides skeletal implementations of some Queue operations.
5 AbstractSequentialList The AbstractSequentialList class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "sequential access" data store (such as a linked list).
6 AbstractSet The AbstractSet class provides a skeletal implementation of the Set interface to minimize the effort required to implement this interface..
7 ArrayDeque The ArrayDeque class Resizable-array implementation of the Deque interface.
8 ArrayList The ArrayList class Resizable-array implementation of the List interface.
9 Arrays The Arrays class contains various methods for manipulating arrays (such as sorting and searching).
10 Collections The Collections class consists exclusively of static methods that operate on or return collections.
11 EnumMap The EnumMap class A specialized Map implementation for use with enum type keys.
12 EnumSet The EnumSet class A specialized Set implementation for use with enum types.
13 HashMap The HashMap class Hash table based implementation of the Map interface.
14 HashSet The HashSet class implements the Set interface, backed by a hash table (actually a HashMap instance).
15 HashTable The HashTable class implements a hash table, which maps keys to values.
16 IdentityHashMap The IdentityHashMap class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values).
17 LinkedHashMap The LinkedHashMap class Hash table and linked list implementation of the Map interface, with predictable iteration order.
18 LinkedHashSet The LinkedHashSet class Hash table and linked list implementation of the Set interface, with predictable iteration order.
19 LinkedList The LinkedList class Doubly-linked list implementation of the List and Deque interfaces.
20 PriorityQueue The PriorityQueue class An unbounded priority queue based on a priority heap.
21 TreeMap The TreeMap class A Red-Black tree based NavigableMap implementation.
22 TreeSet The TreeSet class A NavigableSet implementation based on a TreeMap.
23 Vector The Vector class implements a growable array of objects.
24 WeakHashMap The WeakHashMap class Hash table based implementation of the Map interface, with weak keys.

Java Collections Framework Interfaces

  1. Collection
  2. Set
  3. List
  4. Queue
  5. Deque
  6. Map
  7. SortedSet
  8. SortedMap
  9. NavigableSet
  10. NavigableMap
  11. BlockingQueue
  12. BlockingDeque
  13. ConcurrentMap
  14. ConcurrentNavigableMap