The Java Collections Framework provides a set of classes and interfaces for storing and manipulating groups of data as a single unit. It offers a variety of data structures, including lists, sets, and maps, allowing developers to choose the most appropriate one for their specific needs. Below are three practical examples that illustrate the versatility and utility of the Java Collections Framework.
In this example, we’ll use an ArrayList to manage a dynamic list of students in a school application. The ArrayList allows for easy addition, removal, and access of student records.
import java.util.ArrayList;
public class StudentList {
public static void main(String[] args) {
ArrayList<String> students = new ArrayList<>();
students.add("Alice");
students.add("Bob");
students.add("Charlie");
System.out.println("Students list: " + students);
// Remove a student
students.remove("Bob");
System.out.println("Updated students list: " + students);
// Access a student
String firstStudent = students.get(0);
System.out.println("First student: " + firstStudent);
}
}
ArrayList is ideal for scenarios where frequent access and modification of elements are required.ArrayList can be specified for optimization.This example demonstrates how to use a HashSet to store unique items, such as user IDs, ensuring that there are no duplicates. This is particularly useful in applications where unique identification is critical.
import java.util.HashSet;
public class UniqueIDs {
public static void main(String[] args) {
HashSet<String> userIDs = new HashSet<>();
userIDs.add("user1");
userIDs.add("user2");
userIDs.add("user3");
userIDs.add("user1"); // Duplicate entry
System.out.println("Unique user IDs: " + userIDs);
// Check if a specific ID exists
if (userIDs.contains("user2")) {
System.out.println("User2 exists!");
}
}
}
HashSet does not allow duplicate values and has a constant time complexity for basic operations such as add, remove, and contains.LinkedHashSet if you require the order of insertion.In this example, we utilize a HashMap to store a mapping of product names to their prices in an inventory system. This allows for quick lookups of product prices based on their names.
import java.util.HashMap;
public class ProductInventory {
public static void main(String[] args) {
HashMap<String, Double> inventory = new HashMap<>();
inventory.put("Laptop", 999.99);
inventory.put("Smartphone", 699.99);
inventory.put("Tablet", 299.99);
System.out.println("Product Inventory: " + inventory);
// Retrieve the price of a specific product
double laptopPrice = inventory.get("Laptop");
System.out.println("Price of Laptop: $" + laptopPrice);
// Remove a product
inventory.remove("Tablet");
System.out.println("Updated Inventory: " + inventory);
}
}
HashMap allows for efficient retrieval of values based on their keys, making it suitable for associative arrays.TreeMap or LinkedHashMap depending on the requirements.These examples of Java Collections Framework examples demonstrate how different collection types can be effectively used to manage data in various applications.