Trong hướng dẫn này, chúng ta sẽ tìm hiểu về giao diện Java Map và các phương pháp của nó.
Các Map
giao diện của khuôn khổ bộ sưu tập Java cung cấp các chức năng của cấu trúc dữ liệu bản đồ.
Làm việc của bản đồ
Trong Java, các phần tử của Map
được lưu trữ trong các cặp khóa / giá trị . Khóa là các giá trị duy nhất được liên kết với các Giá trị riêng lẻ .
Bản đồ không được chứa các khóa trùng lặp. Và, mỗi khóa được liên kết với một giá trị duy nhất.
Chúng tôi có thể truy cập và sửa đổi các giá trị bằng cách sử dụng các khóa được liên kết với chúng.
Trong sơ đồ trên, chúng ta có các giá trị: Hoa Kỳ , Brazil và Tây Ban Nha . Và chúng tôi có các khóa tương ứng: us , br và es .
Bây giờ, chúng ta có thể truy cập các giá trị đó bằng các khóa tương ứng của chúng.
Lưu ý: Các Map
giao diện duy trì 3 bộ khác nhau:
- the set of keys
- the set of values
- the set of key/value associations (mapping).
Do đó, chúng tôi có thể truy cập các khóa, giá trị và liên kết riêng lẻ.
Các lớp triển khai Bản đồ
Vì Map
là một giao diện nên chúng ta không thể tạo các đối tượng từ nó.
Để sử dụng các chức năng của Map
giao diện, chúng ta có thể sử dụng các lớp sau:
- HashMap
- EnumMap
- LinkedHashMap
- WeakHashMap
- TreeMap
Các lớp này được định nghĩa trong khung tập hợp và triển khai Map
giao diện.Các lớp con của bản đồ Java
Các giao diện mở rộng Bản đồ
Các Map
giao diện cũng được mở rộng bởi những subinterface:
- SortedMap
- NavigableMap
- ConcurrentMap
Các giao diện con của bản đồ Java
Làm thế nào để sử dụng Bản đồ?
Trong Java, chúng ta phải nhập java.util.Map
gói để sử dụng Map
. Sau khi chúng tôi nhập gói, đây là cách chúng tôi có thể tạo bản đồ.
// Map implementation using HashMap
Map<Key, Value> numbers = new HashMap<>();
Trong đoạn mã trên, chúng tôi đã tạo một sốMap
được đặt tên . Chúng tôi đã sử dụng lớp để triển khai giao diện.HashMap
Map
Đây,
- Key – a unique identifier used to associate each element (value) in a map
- Value – elements associated by keys in a map
Phương pháp bản đồ
Các Map
giao diện bao gồm tất cả các phương pháp của Collection
giao diện. Đó là bởi vì Collection
là một giao diện siêu của Map
.
Bên cạnh các phương thức có sẵn trong Collection
giao diện, Map
giao diện còn bao gồm các phương thức sau:
- put(K, V) – Inserts the association of a key K and a value V into the map. If the key is already present, the new value replaces the old value.
- putAll() – Inserts all the entries from the specified map to this map.
- putIfAbsent(K, V) – Inserts the association if the key K is not already associated with the value V.
- get(K) – Returns the value associated with the specified key K. If the key is not found, it returns
null
. - getOrDefault(K, defaultValue) – Returns the value associated with the specified key K. If the key is not found, it returns the defaultValue.
- containsKey(K) – Checks if the specified key K is present in the map or not.
- containsValue(V) – Checks if the specified value V is present in the map or not.
- replace(K, V) – Replace the value of the key K with the new specified value V.
- replace(K, oldValue, newValue) – Replaces the value of the key K with the new value newValue only if the key K is associated with the value oldValue.
- remove(K) – Removes the entry from the map represented by the key K.
- remove(K, V) – Removes the entry from the map that has key K associated with value V.
- keySet() – Returns a set of all the keys present in a map.
- values() – Returns a set of all the values present in a map.
- entrySet() – Returns a set of all the key/value mapping present in a map.
Triển khai Giao diện Bản đồ
1. Triển khai lớp HashMap
import java.util.Map;
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// Creating a map using the HashMap
Map<String, Integer> numbers = new HashMap<>();
// Insert elements to the map
numbers.put("One", 1);
numbers.put("Two", 2);
System.out.println("Map: " + numbers);
// Access keys of the map
System.out.println("Keys: " + numbers.keySet());
// Access values of the map
System.out.println("Values: " + numbers.values());
// Access entries of the map
System.out.println("Entries: " + numbers.entrySet());
// Remove Elements from the map
int value = numbers.remove("Two");
System.out.println("Removed Value: " + value);
}
}
Đầu ra
Map: {One=1, Two=2} Keys: [One, Two] Values: [1, 2] Entries: [One=1, Two=2] Removed Value: 2
Để tìm hiểu thêm về HashMap
, hãy truy cập Java HashMap .
2. Triển khai lớp TreeMap
import java.util.Map;
import java.util.TreeMap;
class Main {
public static void main(String[] args) {
// Creating Map using TreeMap
Map<String, Integer> values = new TreeMap<>();
// Insert elements to map
values.put("Second", 2);
values.put("First", 1);
System.out.println("Map using TreeMap: " + values);
// Replacing the values
values.replace("First", 11);
values.replace("Second", 22);
System.out.println("New Map: " + values);
// Remove elements from the map
int removedValue = values.remove("First");
System.out.println("Removed Value: " + removedValue);
}
}
Đầu ra
Map using TreeMap: {First=1, Second=2} New Map: {First=11, Second=22} Removed Value: 11
Để tìm hiểu thêm về TreeMap
, hãy truy cập Java TreeMap .