Java
Java
The Java Collection Framework (JCF) is a set of classes and interfaces in the
java.util package that provides data structures and algorithms for storing,
processing, and manipulating data efficiently.
1. Collection Interface
2. List Interface
3. Set Interface
4. Queue Interface
5. Map Interface
Arraylist
The ArrayList is a part of the Java Collection Framework and resides in the
java.util package. It provides a dynamic array that can grow or shrink in size as
elements are added or removed. It is one of the most commonly used data
structures in Java due to its simplicity and efficiency.
Constructors
1. Default Constructor
2. Parameterized Constructor
Method Description
add(E e) Adds an element to the list.
add(int index, E e) Inserts an element at a specific index.
remove(int index) Removes the element at the
specified index.
remove(Object o) Removes the first occurrence of the
specified object.
get(int index) Returns the element at the specified
index.
set(int index, E e) Replaces the element at the specified
index.
size() Returns the number of elements in
the list.
clear() Removes all elements from the list.
contains(Object o) Checks if the list contains a specific
element.
isEmpty() Checks if the list is empty.
indexOf(Object o) Returns the first occurrence index of
an element.
lastIndexOf(Object o) Returns the last occurrence index of
an element.
subList(int from, int to) Returns a portion of the list between
two indices.
import java.util.ArrayList;
Output :
Fruits List After Adding: [Apple, Banana, Cherry]
First Fruit: Apple
Fruits List After Updating: [Apple, Blueberry, Cherry]
Fruits List After Removing by Index: [Apple, Blueberry]
Fruits List After Removing by Value: [Apple]
Contains 'Apple': true
Size of Fruits List: 1
Iterating through the ArrayList:
Apple
Fruits List After Clearing: []
Is Fruits List Empty? true
LinkedList
The LinkedList class in Java is a part of the Java Collection Framework and
resides in the java.util package. It provides a doubly linked list implementation,
allowing efficient insertion and deletion of elements at both ends or in the
middle of the list.
Constructors
1. Default Constructor
2. Collection Constructor
LinkedList<E> list = new LinkedList<>(Collection<? extends E> c);
Creates a linked list containing the elements of the specified collection.
Method Description
add(int index, E
Inserts the element at the specified index.
element)
Code Example
import java.util.LinkedList;
________________________________________
Output
mathematica
Copy code
Initial List: [Apple, Banana, Cherry]
After Adding First and Last: [Mango, Apple, Banana, Cherry, Orange]
First Element: Mango
Last Element: Orange
After Removing First and Last: [Apple, Banana, Cherry]
Contains 'Banana': true
Iterating through the List:
Apple
Banana
Cherry
List After Clearing: []
Is List Empty? true
Stack
A Stack is a linear data structure that follows the LIFO (Last In, First Out)
principle. This means the last element added to the stack is the first one to be
removed. It is commonly used for tasks such as parsing, backtracking, and
evaluating expressions.
Method Description
import java.util.Stack;
Method Description
offer(E e) Inserts the element into the queue; returns false if it fails.
Removes and returns the head of the queue; returns null if queue
poll()
is empty.
Types of Queues
1. Simple Queue:
o Follows FIFO; addition at the rear, removal from the front.
2. Circular Queue:
o Last position connects back to the first to form a circle.
o Solves the limitation of static queue size.
3. Priority Queue:
o Elements are dequeued based on priority, not FIFO order.
4. Double-Ended Queue (Deque):
o Elements can be added or removed from both ends.
Queue Implementations
Using LinkedList
A linked list implementation allows for dynamic resizing.
Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
queue.poll(); // Removes 1
Using ArrayDeque
Provides better performance than LinkedList.
import java.util.PriorityQueue;
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
priorityQueue.add(10);
priorityQueue.add(5);
priorityQueue.add(20);
System.out.println(priorityQueue.poll()); // Removes 5 (smallest)
SET
A Set in Java is a part of the Java Collection Framework that represents a
collection of unique elements. It does not allow duplicate elements and is
typically used when you want to prevent redundancy in a collection.
Set Hierarchy
Common Implementations
1. HashSet:
o Backed by a hash table.
o Does not maintain order.
o Best for frequent add, remove, and contains operations.
Method Description
contains(Object
Checks if the set contains the specified element.
o)
Common Implementations
1. HashMap:
o Backed by a hash table.
o Does not maintain any order of keys.
o Allows one null key and multiple null values.
Method Description
put(K key, V value) Associates the specified key with the specified value.
get(Object key) Returns the value associated with the specified key.
remove(Object key) Removes the key-value pair for the specified key.
containsKey(Object
Checks if the map contains the specified key.
key)
containsValue(Object
Checks if the map contains the specified value.
v)
import java.util.HashMap;
import java.util.Map;
TreeMap Example
import java.util.TreeMap;