Collections in Java
Introduction to Collections
• Collections in Java provide a way to store and manipulate a
group of objects.
• Collections can achieve all the operations that you perform
on a data such as searching, sorting, insertion,
manipulation, and deletion.
• Collections offer a range of data structures like List, Set,
and Map.
Advantages of Using Collections
• Reduces programming effort by providing data structures
and algorithms.
• Provides a standard way to manipulate groups of objects.
• Increases code reusability and reliability.
Key Interfaces in Java Collections Framework
List: An ordered collection (e.g., ArrayList, LinkedList).
Queue: A collection designed for holding elements prior to
processing(PriorityQueue)
Set: A collection that does not allow duplicate elements (e.g.,
HashSet, TreeSet).
Map: A collection of key-value pairs (e.g., HashMap, TreeMap).
List Interface
The List interface in Java is part of the java.util package and
is a sub-interface of the Collection interface
Key Features of the List Interface:
1. Order: The elements in a List are maintained in the order
they are inserted.
2. Duplicate Elements: Lists allow duplicate elements.
3. Index-based Access: You can access, insert, and remove
elements using their index.
Implementations of List :Classes
The List interface has several implementations, including:
1. ArrayList: A resizable array.
2. LinkedList: A doubly linked list.
3. Vector: A synchronized implementation of a dynamic array.
4. Stack: A subclass of Vector that implements a last-in-first-
out (LIFO) stack.
ArrayList
The ArrayList class implements the List interface. It uses a
dynamic array to store the duplicate element of different data
types.
The ArrayList class maintains the insertion order and is non-
synchronized.
The elements stored in the ArrayList class can be randomly
accessed
Syntax:
List<datatype> Object-Name=new ArrayList<datatype>();
ArrayList Methods
• boolean add(Object o)
• int size()
• void clear()
• Object get(int index)
• int indexOf(Object o)
• int LastIndexOf(Object o)
• Object remove(int index)
• boolean remove(Object o
LinkedList
• LinkedList implements the Collection interface. It uses a
doubly linked list internally to store the elements.
• It can store the duplicate elements. It maintains the
insertion order and is not synchronized.
• In LinkedList, the manipulation is fast because no shifting is
required.
LinkedList Methods
• boolean add(Object o)
• int size()
• void addFirst(Object o)
• void addLast(Object o)
• void clear()
• Object get(int index)
• Object getFirst()
• Object getLast()
• int indexOf(Object o)
• int LastIndexOf(Object o)
• Object remove(int index)
• bolean remove(Object o) •
• Object removeFirst()
• Object removeLast(
Set Interface
Set interface is part of the java.util package and is used
to represent a collection of unique elements. It is an
extension of the Collection interface
Features of the Set Interface:
1. No Duplicate Elements: Ensures that no two
elements in the collection are identical.
2. Unordered Collection: Does not guarantee any
specific order of elements
Implementations of Set :Classes
The Set interface has three implementations:
1. HashSet
2. LinkedHashSet
3. TreeSet
HashSet
It is backed by a hash table (an instance of HashMap),
making it a fast and efficient way to store unique elements.
Features of HashSet:
1. No Duplicates: Ensures that no duplicate elements are allowed.
2. No Order Guarantee: Does not maintain any order of elements
(insertion order is not preserved).
3. Allows Null Values: Can store a single null value.
4. Fast Performance: Offers constant-time performance for basic
operations like add, remove, and contains, assuming the hash
function distributes elements evenly
HashSet: Methods
add(E e): Adds the specified element to the set if it is not already present.
remove(Object o): Removes the specified element from the set.
contains(Object o): Checks if the set contains the specified element.
size(): Returns the number of elements in the set.
clear(): Removes all elements from the set.
isEmpty(): Checks if the set is empty.
iterator(): Returns an iterator over the elements in the order they were
inserted.
TreeSet
It uses a red-black tree internally to store elements, ensuring that the set is
sorted in natural order.
Key Features of TreeSet:
Sorted Order: Elements are stored in ascending order by default. You can
provide a custom comparator for a different sorting order.
No Duplicates: Like all sets, TreeSet does not allow duplicate elements.
Efficient Performance: Basic operations like add, remove, and contains
have a time complexity of O(log n) due to the underlying tree structure.
TreeSet: Methods
first(): Returns the first (lowest) element in the set.
last(): Returns the last (highest) element in the set.
ceiling(E e): Returns the smallest element greater than or equal to the given
element, or null if none exists.
floor(E e): Returns the largest element less than or equal to the given element,
or null if none exists.
higher(E e): Returns the smallest element strictly greater than the given
element, or null.
lower(E e): Returns the largest element strictly less than the given element, or
null.
subSet(E fromElement, E toElement): Returns a view of the portion of the set
within the given range.
headSet(E toElement): Returns a view of the portion of the set strictly less than
the given element.
tailSet(E fromElement): Returns a view of the portion of the set greater than or
equal to the given element.
LinkedHashSet
The LinkedHashSet class in Java is a part of the java.util package
and extends the HashSet class while implementing the Set
interface. It maintains a linked list of the elements in the set
Features of LinkedHashSet:
1. Preserves Insertion Order: Unlike HashSet, which does not guarantee
the order of elements, LinkedHashSet maintains the order in which
elements were inserted.
2. No Duplicates: Like all sets, LinkedHashSet does not allow duplicate
elements.
3. Allows Null Values: It permits a single null element.
4. Performance: Similar to HashSet for basic operations (add, remove,
contains) but slightly slower due to the maintenance of the linked list.
LinkedHashSet: Methods
add(E e): Adds the specified element to the set if it is not already present.
remove(Object o): Removes the specified element from the set.
contains(Object o): Checks if the set contains the specified element.
size(): Returns the number of elements in the set.
clear(): Removes all elements from the set.
isEmpty(): Checks if the set is empty.
iterator(): Returns an iterator over the elements in the order they were
inserted.
Summary
Java Collections Framework simplifies the handling of data
structures.
It provides a wide range of interfaces and classes.
Choosing the right collection depends on the use case.
Collections improve code maintainability and efficiency.