COLLECTIONS
import java.util.*;
This java.util package contains all the core collection interfaces
(like Collection, List, Set, Queue, Map) and many common
implementations (like ArrayList, LinkedList, HashSet, TreeSet,
HashMap, TreeMap).
The Collection Framework organizes various collection types
through a hierarchy of interfaces. Here's a breakdown of the
key interfaces:
Collection: The root interface for most collections. It defines
basic operations like add, remove, contains (checks if an
element exists), isEmpty, and size.
List: Represents an ordered collection where elements have a
specific position (index). You can access elements by their
index (like an array). Examples: ArrayList, LinkedList.
Set: Represents an unordered collection where duplicates are
not allowed. Elements are unique. Examples: HashSet, TreeSet.
Queue: Represents a FIFO (First-In-First-Out) structure. The
first element added is the first element removed. Examples:
PriorityQueue (prioritizes elements based on a defined order),
LinkedList (can be used as a queue).
Map: Represents a collection of key-value pairs. Keys are
unique identifiers used to access associated values. Examples:
HashMap, TreeMap.
Common Collection Operations:
Most collections share these common functionalities:
,Adding elements:
add(element): Adds a single element to the collection.
addAll(collection): Adds all elements from another collection.
Removing elements:
remove(element): Removes a specific element if it exists.
removeAll(collection): Removes all elements present in another
collection.
Checking size/emptiness:
size(): Returns the number of elements in the collection.
isEmpty(): Checks if the collection is empty.
Searching:
contains(element): Checks if a specific element exists in the
collection.
indexOf(element): Returns the index of the first occurrence of
an element (applicable to ordered collections like Lists).
Choosing the Right Collection:
The best collection for your needs depends on your specific use
case. Here are some factors to consider:
Ordered vs. Unordered access:
Lists (like ArrayList) provide ordered access. You can access
elements by their position (index).
Sets (like HashSet) are unordered. You cannot access elements
by index.
Duplicates allowed?
Sets don't allow duplicate elements.
Lists and Maps can have duplicates.
Performance considerations:
, ArrayList is efficient for random access (getting elements by
index).
LinkedList is efficient for frequent insertions/deletions in the
middle of the collection.
HashMap is efficient for fast key-based lookups in Maps.
Sample programs :
1.. List
import java.util.ArrayList;
public class StudentList {
public static void main(String[] args) {
// Create an ArrayList of students
ArrayList<String> students = new ArrayList<>();
// Add students to the list
students.add("Alice");
students.add("Bob");
students.add("Charlie");
// Access elements by index
System.out.println("First student: " + students.get(0));
// Check if an element exists
if (students.contains("Bob")) {
System.out.println("Bob is present in the list");
import java.util.*;
This java.util package contains all the core collection interfaces
(like Collection, List, Set, Queue, Map) and many common
implementations (like ArrayList, LinkedList, HashSet, TreeSet,
HashMap, TreeMap).
The Collection Framework organizes various collection types
through a hierarchy of interfaces. Here's a breakdown of the
key interfaces:
Collection: The root interface for most collections. It defines
basic operations like add, remove, contains (checks if an
element exists), isEmpty, and size.
List: Represents an ordered collection where elements have a
specific position (index). You can access elements by their
index (like an array). Examples: ArrayList, LinkedList.
Set: Represents an unordered collection where duplicates are
not allowed. Elements are unique. Examples: HashSet, TreeSet.
Queue: Represents a FIFO (First-In-First-Out) structure. The
first element added is the first element removed. Examples:
PriorityQueue (prioritizes elements based on a defined order),
LinkedList (can be used as a queue).
Map: Represents a collection of key-value pairs. Keys are
unique identifiers used to access associated values. Examples:
HashMap, TreeMap.
Common Collection Operations:
Most collections share these common functionalities:
,Adding elements:
add(element): Adds a single element to the collection.
addAll(collection): Adds all elements from another collection.
Removing elements:
remove(element): Removes a specific element if it exists.
removeAll(collection): Removes all elements present in another
collection.
Checking size/emptiness:
size(): Returns the number of elements in the collection.
isEmpty(): Checks if the collection is empty.
Searching:
contains(element): Checks if a specific element exists in the
collection.
indexOf(element): Returns the index of the first occurrence of
an element (applicable to ordered collections like Lists).
Choosing the Right Collection:
The best collection for your needs depends on your specific use
case. Here are some factors to consider:
Ordered vs. Unordered access:
Lists (like ArrayList) provide ordered access. You can access
elements by their position (index).
Sets (like HashSet) are unordered. You cannot access elements
by index.
Duplicates allowed?
Sets don't allow duplicate elements.
Lists and Maps can have duplicates.
Performance considerations:
, ArrayList is efficient for random access (getting elements by
index).
LinkedList is efficient for frequent insertions/deletions in the
middle of the collection.
HashMap is efficient for fast key-based lookups in Maps.
Sample programs :
1.. List
import java.util.ArrayList;
public class StudentList {
public static void main(String[] args) {
// Create an ArrayList of students
ArrayList<String> students = new ArrayList<>();
// Add students to the list
students.add("Alice");
students.add("Bob");
students.add("Charlie");
// Access elements by index
System.out.println("First student: " + students.get(0));
// Check if an element exists
if (students.contains("Bob")) {
System.out.println("Bob is present in the list");