- What is Collections Framework?
- Key interfaces of Collections Framework
- Comparable & Comparator
The Collections Framework is a core part of the Java Standard Library (Java API) that provides a comprehensive set of interfaces, classes, and algorithms to work with collections of objects. It was introduced in Java 2 (J2SE 1.2) and has since become an integral part of Java for managing and manipulating collections of data. The framework’s primary goals are to provide a consistent and efficient way to handle collections and to reduce the complexity of working with data structures.
What Is Collections Framework?
- Collections Framework consists of set of interfaces and utility classes used for storing, searching and arranging objects.
- Benefits of using this framework are that users don’t have to worry about the number of objects they store, along with availability of number of APIs and Interfaces for easy searching.
The Collections Framework Is Built Around Several key Concepts
- Interfaces : The framework defines a set of core interfaces, such as Collection, List, Set, and Map, that represent various types of collections. These interfaces define the common methods and behaviors expected from different types of collections.
- Classes:Java provides concrete implementations of these interfaces, including
HashMap, and many more. These classes offer different data structures and behaviors to suit various use cases.
- Algorithms: The framework includes utility methods and algorithms for sorting, searching, and manipulating collections. These methods can be used with any class that implements the appropriate interface, allowing for a consistent way to perform common operations.
- Generics:The Collections Framework is designed to work seamlessly with Java’s generics, allowing you to specify the type of elements a collection can hold. This ensures type safety and reduces the need for explicit type casting.
- Iterators:Iterators are used to traverse the elements of a collection. The framework provides iterators for all collection types, making it easy to iterate over the elements regardless of the underlying data structure.
- Concurrency:Java also offers synchronized versions of some collection classes (e.g.,
Hashtable, and synchronized wrappers for other collections) to support multi-threaded programming safely.
Common Collection Interfaces In The Java Collections Framework Include
Collection: The root interface for collections that define basic methods like
List: An ordered collection that allows duplicate elements and provides access by index.
Set: A collection that does not allow duplicate elements.
Map: A collection of key-value pairs, where each key maps to a unique value.
Queue: A collection for managing elements in a FIFO (First-In-First-Out) order.
- Deque: A double-ended queue that supports adding and removing elements from both ends.
- Lists : Store list of objects.
- Sets: Store unique objects.
- Maps : Store objects with unique keys.
- Queues: Store objects in an order, in which they should be processed.
Comparable & Comparator
- java.lang.Comparable is an interface that objects need to implement in case they want to get sorted by Collections class’ sort method.
- For implementing comparable interface, the objects need to override the compareTo(Object o) method.
- This returns 0 if passed object is equal, -1 if passed object is greater and 1 if passed object is lesser
- java.util.Comparator is required, when custom criteria is used for sorting, e.g. sort employee object with name or id or salary or DOJ etc.