Chapter-2 Collections
Chapter-2 Collections
Collections in Java
Last Updated : 03 Jan, 2025
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.
Before the Collection Framework(or before JDK 1.2) was introduced, the
standard methods for grouping Java objects (or collections) were Arrays or
Vectors, or Hashtables. All of these collections had no common interface.
Therefore, though the main aim of all the collections is the same, the
implementation of all these collections was defined independently and had no
correlation among them. And also, it is very difficult for the users to remember
all the different methods, syntax, and constructors present in every collection
class.
Let’s understand this with an example of adding an element in a hashtable and
a vector.
https://www.geeksforgeeks.org/collections-in-java-2/ 2/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
Example:
Java
class CollectionDemo {
https://www.geeksforgeeks.org/collections-in-java-2/ 3/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
Output
1
1
geeks
Since the lack of a collection framework gave rise to the above set of
disadvantages, the following are the advantages of the collection framework.
1. Consistent API: The API has a basic set of interfaces like Collection, Set,
List, or Map, all the classes (ArrayList, LinkedList, Vector, etc) that
implement these interfaces have some common set of methods.
https://www.geeksforgeeks.org/collections-in-java-2/ 4/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
because in this case, the programmer need not think of the best
implementation of a specific data structure. He can simply use the best
implementation to drastically boost the performance of his
algorithm/program.
Interface: Like a class, an interface can have methods and variables, but the
methods declared in an interface are by default abstract (only method
https://www.geeksforgeeks.org/collections-in-java-2/ 5/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
signature, nobody). Interfaces specify what a class must do and not how. It
is the blueprint of the class.
Method Description
https://www.geeksforgeeks.org/collections-in-java-2/ 6/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
Method Description
https://www.geeksforgeeks.org/collections-in-java-2/ 7/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
Method Description
1. Iterable Interface
This is the root interface for the entire collection framework. The collection
interface extends the iterable interface. Therefore, inherently, all the interfaces
and classes implement this interface. The main functionality of this interface is
to provide an iterator for the collections. Therefore, this interface contains only
one abstract method which is the iterator. It returns the
Iterator iterator();
2. Collection Interface
This interface extends the iterable interface and is implemented by all the
classes in the collection framework. This interface contains all the basic
methods which every collection has like adding the data into the collection,
removing the data, clearing the data, etc. All these methods are implemented
in this interface because these methods are implemented by all the classes
irrespective of their style of implementation. And also, having these methods in
this interface ensures that the names of the methods are universal for all the
https://www.geeksforgeeks.org/collections-in-java-2/ 8/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
3. List Interface
For example:
Java
class GFG {
https://www.geeksforgeeks.org/collections-in-java-2/ 9/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
// Main Method
public static void main(String[] args)
{
// Printing elements
System.out.println(al);
Output
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5
ii). LinkedList
The LinkedList class is an implementation of the LinkedList data structure
which 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
https://www.geeksforgeeks.org/collections-in-java-2/ 10/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
and address part. The elements are linked using pointers and addresses. Each
element is known as a node.
Java
class GFG {
// Main Method
public static void main(String[] args)
{
// Printing elements
System.out.println(ll);
https://www.geeksforgeeks.org/collections-in-java-2/ 11/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
Output
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5
iii). Vector
A vector provides us with dynamic arrays in Java. Though, it may be slower
than standard arrays but can be helpful in programs where lots of
manipulation in the array is needed. This is identical to ArrayList in terms of
implementation. However, the primary difference between a vector and an
ArrayList is that a Vector is synchronized and an ArrayList is non-synchronized.
Java
class GFG {
// Main Method
public static void main(String[] args)
{
// Printing elements
System.out.println(v);
https://www.geeksforgeeks.org/collections-in-java-2/ 12/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
v.remove(3);
Output
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5
iv). Stack
Stack class models and implements the Stack data structure. The class is
based on the basic principle of last-in-first-out. In addition to the basic push
and pop operations, the class provides three more functions empty, search, and
peek. The class can also be referred to as the subclass of Vector.
Java
// Main Method
public static void main(String args[])
{
Stack<String> stack = new Stack<String>();
stack.push("Geeks");
stack.push("For");
stack.push("Geeks");
stack.push("Geeks");
https://www.geeksforgeeks.org/collections-in-java-2/ 13/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
System.out.println();
stack.pop();
Output
4. Queue Interface
As the name suggests, a queue interface maintains the FIFO(First In First Out)
order similar to a real-world queue line. This interface is dedicated to storing
https://www.geeksforgeeks.org/collections-in-java-2/ 14/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
all the elements where the order of the elements matter. For example,
whenever we try to book a ticket, the tickets are sold on a first come first serve
basis. Therefore, the person whose request arrives first into the queue gets the
ticket. There are various classes like PriorityQueue, ArrayDeque, etc. Since all
these subclasses implement the queue, we can instantiate a queue object with
any of these classes.
For example:
Java
class GfG {
// Main Method
public static void main(String args[])
{
// Creating empty priority queue
PriorityQueue<Integer> pQueue
= new PriorityQueue<Integer>();
https://www.geeksforgeeks.org/collections-in-java-2/ 15/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
Output
10
10
15
5. Deque Interface
This is a very slight variation of the queue data structure. Deque, also known
as a double-ended queue, is a data structure where we can add and remove
elements from both ends of the queue. This interface extends the queue
interface. The class which implements this interface is ArrayDeque. Since
ArrayDeque class implements the Deque interface, we can instantiate a deque
object with this class.
For example:
https://www.geeksforgeeks.org/collections-in-java-2/ 16/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
ArrayDeque
Java
import java.util.*;
public class ArrayDequeDemo {
public static void main(String[] args)
{
// Initializing an deque
ArrayDeque<Integer> de_que
= new ArrayDeque<Integer>(10);
System.out.println(de_que);
// clear() method
de_que.clear();
System.out.println(de_que);
}
}
Output
6. Set Interface
For example:
The following are the classes that implement the Set interface:
i). HashSet
The HashSet class is an inherent implementation of the hash table data
structure. The objects that we insert into the HashSet do not guarantee to be
inserted in the same order. The objects are inserted based on their hashcode.
https://www.geeksforgeeks.org/collections-in-java-2/ 18/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
This class also allows the insertion of NULL elements. Let’s understand
HashSet with an example:
Java
// Main Method
public static void main(String args[])
{
// Creating HashSet and
// adding elements
HashSet<String> hs = new HashSet<String>();
hs.add("Geeks");
hs.add("For");
hs.add("Geeks");
hs.add("Is");
hs.add("Very helpful");
// Traversing elements
Iterator<String> itr = hs.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output
Very helpful
Geeks
For
Is
ii). LinkedHashSet
https://www.geeksforgeeks.org/collections-in-java-2/ 19/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
Java
// Main Method
public static void main(String args[])
{
// Creating LinkedHashSet and
// adding elements
LinkedHashSet<String> lhs
= new LinkedHashSet<String>();
lhs.add("Geeks");
lhs.add("For");
lhs.add("Geeks");
lhs.add("Is");
lhs.add("Very helpful");
// Traversing elements
Iterator<String> itr = lhs.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output
Geeks
For
https://www.geeksforgeeks.org/collections-in-java-2/ 20/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
Is
Very helpful
This interface is very similar to the set interface. The only difference is that this
interface has extra methods that maintain the ordering of the elements. The
sorted set interface extends the set interface and is used to handle the data
which needs to be sorted. The class which implements this interface is TreeSet.
Since this class implements the SortedSet, we can instantiate a SortedSet
object with this class.
For example:
TreeSet
The TreeSet class uses a Tree for storage. The ordering of the elements is
maintained by a set using their natural ordering whether or not an explicit
comparator is provided. This must be consistent with equals if it is to correctly
implement the Set interface. It can also be ordered by a Comparator provided
at a set creation time, depending on which constructor is used.
Java
// Main Method
public static void main(String args[])
{
https://www.geeksforgeeks.org/collections-in-java-2/ 21/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
ts.add("Geeks");
ts.add("For");
ts.add("Geeks");
ts.add("Is");
ts.add("Very helpful");
// Traversing elements
Iterator<String> itr = ts.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output
For
Geeks
Is
Very helpful
8. Map Interface
A map is a data structure that supports the key-value pair for mapping the
data. This interface doesn’t support duplicate keys because the same key
cannot have multiple mappings, however, it allows duplicate values in different
keys. A map is useful if there is data and we wish to perform operations on the
basis of the key. This map interface is implemented by various classes like
HashMap, TreeMap, etc. Since all the subclasses implement the map, we can
instantiate a map object with any of these classes.
For example:
https://www.geeksforgeeks.org/collections-in-java-2/ 22/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
Java
// Main Method
public static void main(String args[])
{
// Creating HashMap and
// adding elements
HashMap<Integer, String> hm
= new HashMap<Integer, String>();
hm.put(1, "Geeks");
hm.put(2, "For");
hm.put(3, "Geeks");
https://www.geeksforgeeks.org/collections-in-java-2/ 23/31
1/20/25, 9:45 AM Collections in Java - GeeksforGeeks
Output
Similar Reads
Java Tutorial
Java is one of the most popular and widely used programming languages. Used to develop mobile apps,
desktop apps, web apps, web servers, games, and enterprise-level systems. Java was invented by James…
4 min read
Java Overview
Java Basics
Java Methods
Java Arrays
https://www.geeksforgeeks.org/collections-in-java-2/ 24/31