Java Collections Framework
Collections Framework cung cấp các cấu trúc dữ liệu và thuật toán để lưu trữ và xử lý nhóm đối tượng. Hãy cùng tìm hiểu List, Set, Map và khi nào sử dụng chúng!
1. List Interface
Ordered collection, cho phép duplicate elements:
// ArrayList - Dynamic array
List<String> arrayList = new ArrayList<>();
arrayList.add("Java");
arrayList.add("Python");
arrayList.add("Java"); // Duplicate OK
System.out.println(arrayList.get(0)); // "Java"
// LinkedList - Doubly linked list
List<String> linkedList = new LinkedList<>();
linkedList.add("First");
linkedList.add("Second");
linkedList.addFirst("New First"); // LinkedList specific
// Vector - Thread-safe (legacy)
List<String> vector = new Vector<>();
vector.add("Item");
| Implementation | Ưu điểm | Nhược điểm | Khi nào dùng |
|---|---|---|---|
| ArrayList | Truy cập nhanh O(1) | Thêm/xóa giữa list chậm | Đọc nhiều, sửa ít |
| LinkedList | Thêm/xóa nhanh | Truy cập chậm O(n) | Thêm/xóa nhiều |
2. Set Interface
Không cho phép duplicate elements:
// HashSet - Không có thứ tự, nhanh nhất
Set<String> hashSet = new HashSet<>();
hashSet.add("Java");
hashSet.add("Python");
hashSet.add("Java"); // Bị bỏ qua
System.out.println(hashSet.size()); // 2
// LinkedHashSet - Giữ thứ tự insertion
Set<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("C");
linkedHashSet.add("A");
linkedHashSet.add("B");
// Iteration: C, A, B (theo thứ tự thêm)
// TreeSet - Tự động sắp xếp
Set<String> treeSet = new TreeSet<>();
treeSet.add("C");
treeSet.add("A");
treeSet.add("B");
// Iteration: A, B, C (alphabetical)
3. Map Interface
Lưu trữ key-value pairs:
// HashMap - Nhanh nhất, không có thứ tự
Map<String, Integer> hashMap = new HashMap<>();
hashMap.put("Java", 1);
hashMap.put("Python", 2);
hashMap.put("JavaScript", 3);
System.out.println(hashMap.get("Java")); // 1
hashMap.containsKey("Python"); // true
// LinkedHashMap - Giữ thứ tự
Map<String, Integer> linkedHashMap = new LinkedHashMap<>();
// TreeMap - Tự động sắp xếp theo key
Map<String, Integer> treeMap = new TreeMap<>();
// Iteration
for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
4. Queue Interface
FIFO (First-In-First-Out) structure:
// LinkedList as Queue
Queue<String> queue = new LinkedList<>();
queue.offer("First");
queue.offer("Second");
queue.poll(); // Lấy và xóa "First"
queue.peek(); // Xem "Second" không xóa
// PriorityQueue - Tự động sắp xếp
Queue<Integer> pq = new PriorityQueue<>();
pq.offer(5);
pq.offer(1);
pq.offer(3);
pq.poll(); // 1 (nhỏ nhất)
5. Khi nào dùng gì?
| Nhu cầu | Sử dụng |
|---|---|
| Lưu danh sách có thứ tự, cho phép duplicate | ArrayList |
| Thêm/xóa phần tử thường xuyên | LinkedList |
| Không duplicate, không cần thứ tự | HashSet |
| Không duplicate, cần sắp xếp | TreeSet |
| Key-value, tra cứu nhanh | HashMap |
| Key-value, cần sắp xếp theo key | TreeMap |
| FIFO queue | LinkedList (as Queue) |
| Priority queue | PriorityQueue |
6. Utility Methods
// Collections utility class
List<Integer> list = Arrays.asList(3, 1, 4, 1, 5);
Collections.sort(list); // Sắp xếp
Collections.reverse(list); // Đảo ngược
Collections.shuffle(list); // Xáo trộn
Collections.max(list); // Tìm max
Collections.min(list); // Tìm min
Collections.binarySearch(list, 3); // Tìm kiếm nhị phân
Kết luận
Hiểu rõ Collections Framework giúp bạn chọn cấu trúc dữ liệu phù hợp, tối ưu performance. Hãy nhớ:
- ArrayList cho random access
- LinkedList cho frequent insertion/deletion
- HashSet/HashMap khi cần tốc độ cao
- TreeSet/TreeMap khi cần sắp xếp