A Java Collection of Objects refers to any set of independent objects represented as a single entity.
A framework is a collection of classes and interfaces that form a ready-made architecture. A framework does not need to be defined before implementing a new feature or class.
Collection Framework offers an architecture for storing and manipulating a set of objects. It makes it easier to develop code by offering data structures and algorithms. It delivers high-performance and high-quality data structures, enhancing speed. It promotes the reuse of standard data structures and methods.
The Java Collection Framework consists of the following:
The collection framework includes several interfaces, each of which is used to store certain types of data. The following are the interfaces included in the framework:
The Collection Interface is the core part of the Java Collections Framework, operating as the root interface. While no class directly implements it, it serves as the foundation for sub-interfaces such as List, Set, and Queue, which are then implemented by framework classes.
The Iterator interface allows for sequential traversal of a collection of elements. It is implemented by all collection classes, including ArrayList, LinkedList, HashSet, and TreeSet.
The Iterator interface includes 3 basic methods:
The Java Iterable interface offers uniform iteration over collection elements, which simplifies generic code. It facilitates traversal by allowing activities such as element reading, removal, and manipulation, while also serving as a universal cursor for the Collection API with user-friendly functions.
The Java List interface provides ordered storage for elements, including duplicates, and is commonly utilized for data manipulation. It is found in the java.util package and inherits from the Collection interface, allowing for a standardized approach to organizing ordered collections.
The classes implementing the List interface are as follows:
This class implements the List interface using a resizable array. It is identical to an array, except its size changes dynamically when elements are added or withdrawn.
The LinkedList class implements the LinkedList data structure, which is a linear data structure in which items are not stored in contiguous locations and each element is a separate object having data and address parts. The elements are linked by pointers and addresses. Each element is called a node.
In Java, we may create dynamic arrays using vectors. Though slower than standard arrays, it might be useful in programs that require a lot of array operations. This is implemented in the same way as ArrayList.
The Stack class defines and implements the Stack data structure. The class operates on the basic concept of last-in, first-out. In addition to the standard push and pop operations, the class includes three additional functions: empty, search, and peek. The class is also known as a subclass of Vector.
The Comparable interface in Java, available in the java.lang package generates a natural ordering of objects through its single method, 'compareTo()'. This method compares items of the same type and returns a negative, zero, or positive integer based on their relative order.
The Queue Interface implements the queue data structure's functionality. It is included in the java.util package and extends the Collection Interface. It is used to retain the elements that will be processed in the FIFO (First In, First Out) order.
The classes implementing the Queue interface are as follows:
A set interface is an unordered collection of distinct items. The Java Collections Framework includes a built-in Set interface, which is implemented by several classes including HashSet, TreeSet, and LinkedHashSet.
HashSet uses a process known as hashing to store its elements. HashSet is made up of unique components. The elements are sorted automatically.
The LinkedHashSet class implements the set interface using a hash table and a linked list. It only contains unique elements, similar to HashSet. Linked HashSet additionally maintains insertion order.
The TreeSet class implements the Set interface and stores data in a tree structure. The objects of this class are stored in ascending order. The TreeSet class provides faster access and retrieval times.
A Map is a collection of key-value pairs, with each key associated with a value. The Map interface maps unique keys to values.
Some exceptions are thrown when using the Maps interface:
HashMap is an implementation of the Map interface. In a HashMap, the keys and values can be any Object type, but the keys must be unique. The HashMap holds key-value pairs in a hash table, allowing for quick lookup and retrieval of items based on their keys.