0% found this document useful (0 votes)
2 views

Java_Collections_Guide

Java's Collection Framework provides a structured way to manage groups of objects, allowing operations like storing, adding, removing, and sorting elements. The main types of collections include List, Set, Map, and Queue, each serving different purposes such as maintaining order, ensuring uniqueness, or storing key-value pairs. Collections enhance data management efficiency and flexibility, catering to various programming needs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java_Collections_Guide

Java's Collection Framework provides a structured way to manage groups of objects, allowing operations like storing, adding, removing, and sorting elements. The main types of collections include List, Set, Map, and Queue, each serving different purposes such as maintaining order, ensuring uniqueness, or storing key-value pairs. Collections enhance data management efficiency and flexibility, catering to various programming needs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

In Java, a collection is a group of objects stored together.

It allows you to store, manipulate, and process data in an


organized way. Collections are used to handle a group of objects as a single unit.

Java provides a Collection Framework that includes several classes and interfaces to manage collections of objects.
These collections allow you to:

Store data.
Add and remove elements.
Find elements.
Sort elements.
And much more!

Main Types of Collections in Java:

List: A collection that stores elements in a specific order. You can access elements by their position (index) in the list.

Example: A shopping list where the order matters (first item, second item, etc.).

Key classes:
ArrayList: A dynamic array that grows and shrinks.
LinkedList: A list where elements are linked together (better for adding/removing in the middle).

Example:
List<String> shoppingList = new ArrayList<>();
shoppingList.add("Milk");
shoppingList.add("Eggs");
shoppingList.add("Bread");

Set: A collection that does not allow duplicates. It does not care about the order of elements.

Example: A set of unique students' IDs, where no ID is repeated.

Key classes:
HashSet: Does not maintain any specific order.
LinkedHashSet: Maintains the order of insertion (the order in which elements were added).
TreeSet: Stores elements in sorted order.

Example:
Set<String> uniqueNames = new HashSet<>();
uniqueNames.add("John");
uniqueNames.add("Alice");
uniqueNames.add("John"); // Duplicate, will not be added

Map: A collection of key-value pairs, where each key is unique and maps to exactly one value.

Example: A dictionary where each word (key) has a definition (value).

Key classes:
HashMap: Stores key-value pairs with no specific order.
LinkedHashMap: Maintains insertion order of the key-value pairs.
TreeMap: Stores key-value pairs in sorted order based on keys.

Example:
Map<String, String> dictionary = new HashMap<>();
dictionary.put("Apple", "A fruit");
dictionary.put("Banana", "Another fruit");

Queue: A collection that stores elements in a First-In-First-Out (FIFO) order, like a queue at a ticket counter.

Example: A line of people waiting for a bus.

Key classes:
LinkedList: Can be used as a queue.
PriorityQueue: Orders elements based on priority, not the order they were added.

Example:
Queue<String> queue = new LinkedList<>();
queue.add("Alice");
queue.add("Bob");
queue.poll(); // Removes the first element (Alice)

Common Operations with Collections:

Add: To add elements to a collection.


Example: list.add("Apple");

Remove: To remove elements from a collection.


Example: list.remove("Apple");

Size: To get the number of elements in a collection.


Example: list.size();

Check if Empty: To check if a collection is empty.


Example: list.isEmpty();

Loop through elements: To access each element in a collection.


Example:
for (String item : shoppingList) {
System.out.println(item);
}

Why Use Collections?

Efficiency: Collections help you store and access data more efficiently. For example, HashSet allows quick lookups,
and ArrayList allows fast access by index.

Flexibility: Java provides different types of collections (list, set, map, queue) for different needs. For example, use a
Set when you don't want duplicates, or a Map when you need key-value pairs.

Simple Example of Using Collections:

import java.util.*;

public class Main {


public static void main(String[] args) {
// List (Ordered, Allows Duplicates)
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple"); // Duplicates allowed
System.out.println("List: " + fruits);

// Set (Unordered, No Duplicates)


Set<String> uniqueFruits = new HashSet<>();
uniqueFruits.add("Apple");
uniqueFruits.add("Banana");
uniqueFruits.add("Apple"); // Duplicate will be ignored
System.out.println("Set: " + uniqueFruits);

// Map (Key-Value Pairs)


Map<String, Integer> fruitPrices = new HashMap<>();
fruitPrices.put("Apple", 3);
fruitPrices.put("Banana", 1);
System.out.println("Map: " + fruitPrices);

// Queue (FIFO)
Queue<String> queue = new LinkedList<>();
queue.add("John");
queue.add("Alice");
System.out.println("Queue (Before poll): " + queue);
queue.poll(); // Removes the first element
System.out.println("Queue (After poll): " + queue);
}
}

Output:

List: [Apple, Banana, Apple]


Set: [Apple, Banana]
Map: {Apple=3, Banana=1}
Queue (Before poll): [John, Alice]
Queue (After poll): [Alice]

Conclusion:

Collections in Java are simply groups of objects. Depending on your needs (ordering, duplicates, key-value pairs),
you choose different types of collections like List, Set, Map, and Queue.

They help manage data more efficiently, whether you're storing items, ensuring uniqueness, or associating keys with
values.

You might also like