Collections in Java
Any group of individual objects that are represented as a single unit is known as a
Java Collection of Objects. In Java, a separate framework named the "Collection
Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and
Interface in it.
In Java, the Collection interface ([Link]) and Map interface
([Link]) are the two main “root” interfaces of Java collection classes.
What is a Framework in Java?
A framework is a set of classes and interfaces which provide a ready-made
architecture. In order to implement a new feature or a class, there is no need to define a
framework. However, an optimal object-oriented design always includes a framework with a
collection of classes such that all the classes perform the same kind of task.
Before understanding the different components in the above framework, let's first understand
a class and an interface.
Class: A class is a user-defined blueprint or prototype from which objects are created.
It represents the set of properties or methods that are common to all objects of one
type.
Interface: Like a class, an interface can have methods and variables, but the methods
declared in an interface are by default abstract (only method signature, nobody).
Interfaces specify what a class must do and not how. It is the blueprint of the class.
Java List Interface
The List Interface in Java extends the Collection Interface and is a part of the [Link]
package. It is used to store the ordered collections of elements. In a Java List, we can
organize and manage the data sequentially.
Key Features:
Maintained the order of elements in which they are added.
Allows duplicate elements.
The implementation classes of the List interface are ArrayList, LinkedList, Stack,
and Vector.
The List interface offers methods to access elements by their index
Syntax of List Interface:
List<Obj> list = new ArrayList<Obj> ();
1) ADD:
2) SET: (Update)
set(int index, object o) - Since List is indexed, the element which
we wish to change is referenced by the index of the element.
3) SEARCH:
4) REMOVE:
5) ACCESSING ELEMENTS:
6) CHECKING:
OTHERS:
1. ArrayList
An ArrayList class which is implemented in the collection framework provides us
with dynamic arrays in Java. It may be slower than standard arrays but can be helpful in
programs where lots of manipulation in the array is needed.
ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for
such cases.
2. Vector
Vector is a class that is implemented in the collection framework implements a
growable array of objects. Vector implements a dynamic array that means it can grow or
shrink as required. Like an array, it contains components that can be accessed using an
integer index.
3. Stack
Stack is a class that is implemented in the collection framework and extends the
vector class models and implements the Stack data structure. The class is based on the basic
principle of last-in-first-out (LIFO). With basic push and pop operations, the class
provides three more functions of empty, search and peek.
4. LinkedList
LinkedList is a class that is implemented in the collection framework which
inherently implements the linked list data structure. It is a linear data structure where the
elements are not stored in contiguous locations and every element is a separate object
with a data part and address part.
The elements are linked using pointers and addresses, and each element is
known as a node.
Set:
The Set Interface is present in [Link] package and extends the Collection interface.
It is an unordered collection of objects in which duplicate values cannot be stored. It is an
interface that implements the mathematical set. This interface adds a feature that restricts the
insertion of duplicate elements.
We’ll proceed in this manner:
HashSet
LinkedHashSet
TreeSet
Java HashSet
HashSet in Java implements the Set interface of Collections Framework. It is used to
store the unique elements and it doesn't maintain any specific order of elements.
Java LinkedHashSet
LinkedHashSet in Java implements the Set interface of the Collection Framework. It
combines the functionality of a HashSet with a LinkedList to maintain the insertion order of
elements.