0% found this document useful (0 votes)
2 views5 pages

Java Collections

The document provides an overview of various Java collections, including Set, HashMap, LinkedList, ArrayList, Stack, and Queue, detailing their characteristics and use cases. Each collection type is accompanied by example code demonstrating its functionality. The summary highlights the unique features and appropriate scenarios for using each collection.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Java Collections

The document provides an overview of various Java collections, including Set, HashMap, LinkedList, ArrayList, Stack, and Queue, detailing their characteristics and use cases. Each collection type is accompanied by example code demonstrating its functionality. The summary highlights the unique features and appropriate scenarios for using each collection.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Absolutely!

Let's dive deeper into each collection, providing more context, use cases, and
detailed examples.

Collections in Java

1. Set

A Set is a collection that cannot contain duplicate elements. It models the mathematical set
abstraction. The primary implementations of the Set interface include:

●​ HashSet: Stores elements in a hash table, offers constant time performance for basic
operations.
●​ LinkedHashSet: Maintains a doubly-linked list across all elements, orders elements
in the order they were inserted.
●​ TreeSet: Implements the SortedSet interface, uses a tree for storage, orders
elements based on their values.

Example using HashSet:

java
Copy code
import [Link];
import [Link];

public class SetExample {


public static void main(String[] args) {
Set<String> set = new HashSet<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Orange");
[Link]("Apple"); // Duplicate element

for (String fruit : set) {


[Link](fruit);
}
// Output: Unordered, no duplicates (e.g., Banana, Orange,
Apple)
}
}

Use Case: Use Set when you need to maintain a collection of unique elements, such as a
list of unique IDs.

2. HashMap
A HashMap stores key-value pairs. It allows fast retrieval of values based on their keys and
permits null values and the null key.

Example using HashMap:

java
Copy code
import [Link];
import [Link];

public class HashMapExample {


public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
[Link](1, "One");
[Link](2, "Two");
[Link](3, "Three");

for ([Link]<Integer, String> entry : [Link]()) {


[Link]("Key: " + [Link]() + ", Value:
" + [Link]());
}
// Output:
// Key: 1, Value: One
// Key: 2, Value: Two
// Key: 3, Value: Three
}
}

Use Case: Use HashMap when you need a dictionary-like structure for fast lookups, such as
mapping user IDs to user details.

3. LinkedList

A LinkedList implements both the List and Deque interfaces. It is a doubly-linked list
which allows for efficient insertions and deletions.

Example using LinkedList:

java
Copy code
import [Link];

public class LinkedListExample {


public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
[Link]("A");
[Link]("B");
[Link]("C");

[Link]("Start");
[Link]("End");

for (String item : list) {


[Link](item);
}
// Output:
// Start
// A
// B
// C
// End
}
}

Use Case: Use LinkedList when you need a list with frequent additions and deletions,
particularly at the ends of the list.

4. ArrayList

An ArrayList is a resizable array implementation of the List interface. It provides fast


random access and is the most commonly used list implementation.

Example using ArrayList:

java
Copy code
import [Link];
import [Link];

public class ArrayListExample {


public static void main(String[] args) {
List<String> list = new ArrayList<>();
[Link]("X");
[Link]("Y");
[Link]("Z");

for (int i = 0; i < [Link](); i++) {


[Link]([Link](i));
}
// Output:
// X
// Y
// Z
}
}

Use Case: Use ArrayList for lists that require frequent access to elements by index and
when the number of elements is not fixed.

5. Stack

A Stack is a collection that follows the Last-In-First-Out (LIFO) principle. The Stack class
extends Vector with five operations that allow a vector to be treated as a stack.

Example using Stack:

java
Copy code
import [Link];

public class StackExample {


public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
[Link](1);
[Link](2);
[Link](3);

[Link]("Top element: " + [Link]()); //


Output: 3
[Link]("Popped element: " + [Link]()); //
Output: 3
[Link]("Top element after pop: " +
[Link]()); // Output: 2
}
}

Use Case: Use Stack when you need a last-in-first-out (LIFO) data structure, such as for
implementing undo functionality in applications.

6. Queue
A Queue is a collection that follows the First-In-First-Out (FIFO) principle. The Queue
interface is part of the Java Collections Framework and has several implementations such
as LinkedList and PriorityQueue.

Example using LinkedList as Queue:

java
Copy code
import [Link];
import [Link];

public class QueueExample {


public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
[Link]("First");
[Link]("Second");
[Link]("Third");

[Link]("Head of queue: " + [Link]()); //


Output: First
[Link]("Removed from queue: " + [Link]());
// Output: First
[Link]("Head of queue after removal: " +
[Link]()); // Output: Second
}
}

Use Case: Use Queue for storing data in the order it needs to be processed, such as in
scheduling tasks or managing requests in a server.

Summary

●​ Set: Collection of unique elements, e.g., HashSet.


●​ HashMap: Key-value pairs, fast lookup, e.g., HashMap.
●​ LinkedList: Doubly-linked list, efficient insertions/deletions, e.g., LinkedList.
●​ ArrayList: Resizable array, fast random access, e.g., ArrayList.
●​ Stack: LIFO data structure, e.g., Stack.
●​ Queue: FIFO data structure, e.g., LinkedList.

These collections provide various ways to store and manage groups of objects, each with its
strengths and suited use cases.

You might also like