In Java, the term "Collections" refers to data structures that store and manipulate groups of objects. The Java Collections Framework provides several interfaces and classes for implementing and organizing collections. Here are the main types of collections in Java along with examples:
1. List Interface:
Description: Lists are ordered collections that allow duplicate elements.
Example Code:
import java.util.List;
import java.util.ArrayList;
public class ListExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
// Adding elements to the list
names.add("Alice");
names.add("Bob");
names.add("Charlie");
// Accessing elements by index
System.out.println("First name: " + names.get(0));
// Iterating through the list
System.out.println("Names:");
for (String name : names) {
System.out.println(name);
}
}
}
2. Set Interface:
Description: Sets are unordered collections that do not allow duplicate elements.
Example Code:
import java.util.Set;
import java.util.HashSet;
public class SetExample {
public static void main(String[] args) {
Set<String> uniqueNames = new HashSet<>();
// Adding elements to the set
uniqueNames.add("Alice");
uniqueNames.add("Bob");
uniqueNames.add("Alice"); // Ignored, as "Alice" already exists
// Iterating through the set
System.out.println("Unique Names:");
for (String name : uniqueNames) {
System.out.println(name);
}
}
}
3. Map Interface:
Description: Maps store key-value pairs, where each key is associated with exactly one value.
Example Code:
import java.util.Map;
import java.util.HashMap;
public class MapExample {
public static void main(String[] args) {
Map<String, Integer> ageMap = new HashMap<>();
// Adding key-value pairs to the map
ageMap.put("Alice", 30);
ageMap.put("Bob", 25);
ageMap.put("Charlie", 35);
// Accessing values by key
System.out.println("Bob's Age: " + ageMap.get("Bob"));
// Iterating through the map
System.out.println("Ages:");
for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
4. Queue Interface:
Description: Queues represent a collection used to hold multiple elements before processing.
Example Code:
import java.util.Queue;
import java.util.LinkedList;
public class QueueExample {
public static void main(String[] args) {
Queue<String> waitingQueue = new LinkedList<>();
// Enqueuing elements
waitingQueue.offer("Customer 1");
waitingQueue.offer("Customer 2");
waitingQueue.offer("Customer 3");
// Dequeuing elements
System.out.println("Processing: " + waitingQueue.poll());
System.out.println("Processing: " + waitingQueue.poll());
}
}
5. Stack Class:
Description: A stack is a collection that uses the Last-In-First-Out (LIFO) order.
Example Code:
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<String> webHistory = new Stack<>();
// Pushing elements onto the stack
webHistory.push("Page 1");
webHistory.push("Page 2");
webHistory.push("Page 3");
// Popping elements from the stack
System.out.println("Back: " + webHistory.pop());
System.out.println("Back: " + webHistory.pop());
}
}
These examples showcase various types of collections in Java along with their basic operations. The Java Collections Framework provides a rich set of interfaces and classes that can be utilized to solve a wide range of programming problems involving collections of data.