Java Collections
Working with groups of objects in Java
π¦ What are Java Collections?
Java Collections are frameworks that provide data structures like lists, sets, and maps to store and manipulate groups of objects efficiently with built-in methods.
// Simple ArrayList example
import java.util.ArrayList;
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
System.out.println(fruits); // [Apple, Banana]
Types of Collections
ArrayList
Dynamic arrays that can grow
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
LinkedList
Doubly-linked list implementation
LinkedList<String> names = new LinkedList<>();
names.addFirst("John");
names.addLast("Jane");
HashSet
Unique elements only
HashSet<String> colors = new HashSet<>();
colors.add("Red");
colors.add("Blue");
HashMap
Key-value pairs storage
HashMap<String, Integer> ages = new HashMap<>();
ages.put("Alice", 25);
ages.put("Bob", 30);
πΉ ArrayList Methods
Common methods for working with ArrayLists:
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
// Adding elements
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// Getting elements
System.out.println("First fruit: " + fruits.get(0));
// Size of list
System.out.println("Total fruits: " + fruits.size());
// Removing elements
fruits.remove("Banana");
// Check if contains
if (fruits.contains("Apple")) {
System.out.println("We have apples!");
}
// Print all fruits
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
Output:
First fruit: Apple
Total fruits: 3
We have apples!
Apple
Orange
Total fruits: 3
We have apples!
Apple
Orange
πΉ HashMap Example
Working with key-value pairs:
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
HashMap<String, Integer> studentGrades = new HashMap<>();
// Adding key-value pairs
studentGrades.put("Alice", 95);
studentGrades.put("Bob", 87);
studentGrades.put("Charlie", 92);
// Getting values
System.out.println("Alice's grade: " + studentGrades.get("Alice"));
// Check if key exists
if (studentGrades.containsKey("Bob")) {
System.out.println("Bob's grade found!");
}
// Print all entries
for (String name : studentGrades.keySet()) {
System.out.println(name + ": " + studentGrades.get(name));
}
}
}
Output:
Alice's grade: 95
Bob's grade found!
Alice: 95
Bob: 87
Charlie: 92
Bob's grade found!
Alice: 95
Bob: 87
Charlie: 92
πΉ HashSet Example
Working with unique elements:
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<String> uniqueColors = new HashSet<>();
// Adding elements
uniqueColors.add("Red");
uniqueColors.add("Blue");
uniqueColors.add("Red"); // Duplicate - won't be added
System.out.println("Colors: " + uniqueColors);
System.out.println("Size: " + uniqueColors.size());
// Check if contains
if (uniqueColors.contains("Blue")) {
System.out.println("Blue is in the set!");
}
}
}
Output:
Colors: [Red, Blue]
Size: 2
Blue is in the set!
Size: 2
Blue is in the set!