A popular framework that provides an architecture to store and manipulate a group of objects. This framework has been introduced in JDK version 1.2 and contains all classes and interfaces.
There are 2 main root interfaces —
- The Collection interface ( java.util.Collection )
- The Map interface ( java.util.Map )
Before the introduction of the Collection framework, developers had to remember different methods, syntaxes, and various constructors present in different classes such as Arrays, Vectors, and Hashtables(classes that provide methods for grouping objects or collections before JDK 1.2). This was almost impossible and resulted in various problems. Developers had to write many different algorithms in order to do work with these early collections classes.
Advantages of Collection Framework
- Reduce Programming effort.
- Increases the speed and quality of the program.
- Improve consistency.
Collection Framework Hierarchy
- This is the root interface for all the collections in the API and it is placed at the top of the collection hierarchy.
- Provides basic operations for adding and removing elements.
- This interface extends the iterable interface, which has 3 methods. They are iterator, spliterator (splitting + iterator), and foreach(Consumer<? super T> action). However, we can only implement the Iterator method. The other 2 methods have default implementations. This iterator method returns an iterator object and that object can be used to iterate over the available elements inside a collection.
- List, queue, and Set interfaces extend the Collection Interface.
- Responsible for declaring the behavior of collections which stores a sequence of elements. A list may contain duplicate elements and the order is retained in which we add elements. That means we can retrieve elements according to this order.
- There are 3 main subclasses that implement the list interface.
- The list defines some of its own methods as follows.
- Void add(int index, Object obj): Insert Object into the specified index.
- boolean addAll(int index, Collection c): Insert all elements of Collection c into the location of the specified index.
- Object get(int index): Return object stored at the specified index.
- int indexOf(Object obj): Return the index of the first instance of the object.
- int lastIndexOf(Object obj): Return the index of the last instance of the object.
- ListIterator listIterator(): Returns an iterator.
- ListInterator list iterator(int index): Returns an iterator that will invoke a list that begins at a specified index.
- Object remove(int index): Remove an element at the specified index.
- Object set(int index, object obj): Assign object to the location specified by the index.
- List subList(int start, int end): Returns list of elements from specified start to end.
Set is a special type of collection where we cannot store duplicate elements. This interface once contains the methods inherited from the Collection interface. This is an unordered collection of objects. Mathematical operations such as intersection, union, and difference are also supported in Set interface.
There are 2 main subclasses that implement the Set interface as it is.
This is implemented using the SortedSet interface. SortedSet is another special class that extends the Set interface. TreeSet uses a tree data structure to store the data.
SortedSet — Behaves like a simple set with the exception that it stores elements in a sorted format.
- add(element): All specific element to the set.
- addAll(collection) : Append all elements from the provided collection to the set.
- clear() : Remove all elements from the set.
- contains(element) : Check for a specific element in the set.
- containsAll(collection) : Check whether the set contains all elements present in the given set.
- hashCode() : Get the hashcode value.
- isEmpty() : Check whether the set is empty.
- iterator() : Return the iterator of the set.
- remove(element) : Remove given element from the set.
- removeAll(collection) : Remove all elements from a collection
- retainAll(collection) : Retain all elements from a set.
- size() : Get the size of the set.
- toArray() : Form an array using the elements in the set.
Holds elements that are going to be processed in a special order called FIFO(First In First Out). In queues, we can insert elements from one end of the list and take out elements from the other end.
Queue Interface is implemented by a single class known as the PriorityQueue. However, this interface is extended by another interface call Deque which implemented the ArrayDeque class.
Dequeue stands for “double-ended queue”.In a double-ended queue, we can add or remove elements from either end of the queue. This increases the versatility of the queue. We can use the deque as a normal queue that follows FIFO. But most of the time deques are used to act as a stack that follows LIFO(Last In First Out). ArrayQue class implements the Deque interface.
Methods of Queue Interface
- add(element): Add elements to the end of the queue.
- element(): View the head of the element. ( Throw NoSuchElementException when the queue is empty)
- offer(element): Insert an element into the queue. Returns false when the container is full.
- peek(): View the head (top element) of the queue.
- poll(): Removes and returns the head of the queue.
- remove(): Removes and returns the head of the queue(Throw NoSuchElementException when the queue is empty).
Map interface behaves differently from the rest of the collection types. Map stores its elements as key/value pairs. Each object is mapped to a particular key. There cannot be any duplicate keys in Map. But we can have duplicate values.
The Map is ideal for searching, updating, and deleting records based on a key.
The next section of this article will describe more about these individual classes in the Collection framework and how to implement them.
Map (Java Platform SE 8 )
Many methods in Collections Framework interfaces are defined in terms of the method. For example, the specification for…
Set in Java - GeeksforGeeks
The set interface present in the java.util package and extends the Collection interface is an unordered collection of…