Giao diện Java ConcurrentMap

Trong hướng dẫn này, chúng ta sẽ tìm hiểu về giao diện Java ConcurrentMap và các phương thức của nó.

Các ConcurrentMapgiao diện của khuôn khổ bộ sưu tập Java cung cấp một bản đồ thread-safe. Có nghĩa là, nhiều luồng có thể truy cập bản đồ cùng một lúc mà không ảnh hưởng đến tính nhất quán của các mục trong bản đồ.

ConcurrentMap được biết đến như một bản đồ đồng bộ.

Nó mở rộng giao diện Bản đồ .

Lớp triển khai Bản đồ đồng thời

Vì ConcurrentMaplà 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 ConcurrentMapgiao diện, chúng ta cần sử dụng lớp ConcurrentHashMaptriển khai nó.

Làm thế nào để sử dụng ConcurrentMap?

Để sử dụng ConcurrentMap, chúng ta phải nhập java.util.concurrent.ConcurrentMapgói trước. Sau khi chúng tôi nhập gói, đây là cách chúng tôi có thể tạo một bản đồ đồng thời.

// ConcurrentMap implementation by ConcurrentHashMap
CocurrentMap<Key, Value> numbers = new ConcurrentHashMap<>();

Trong đoạn mã trên, chúng ta đã tạo một bản đồ đồng thời có tên là các con số .

Đây,

  • Key – a unique identifier used to associate each element (value) in a map
  • Value – elements associated by keys in a map

Phương thức Bản đồ đồng thời

Các ConcurrentMapgiao diện bao gồm tất cả các phương pháp của Mapgiao diện. Đó là bởi vì Maplà siêu giao diện của ConcurrentMapgiao diện.

Bên cạnh tất cả các phương pháp đó, đây là các phương thức dành riêng cho ConcurrentMapgiao diện.

  • putIfAbsent() – Inserts the specified key/value to the map if the specified key is not already associated with any value.
  • compute() – Computes an entry (key/value mapping) for the specified key and its previously mapped value.
  • computeIfAbsent() – Computes a value using the specified function for the specified key if the key is not already mapped with any value.
  • computeIfPresent() – Computes a new entry (key/value mapping) for the specified key if the key is already mapped with the specified value.
  • forEach() – Access all entries of a map and perform the specified actions.
  • merge() – Merges the new specified value with the old value of the specified key if the key is already mapped to a certain value. If the key is not already mapped, the method simply associates the specified value to our key.

Để tìm hiểu thêm, hãy truy cập Java ConcurrentMap (tài liệu Java chính thức) .

Triển khai ConcurrentMap trong ConcurrentHashMap

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;

class Main {

    public static void main(String[] args) {
        // Creating ConcurrentMap using ConcurrentHashMap
        ConcurrentMap<String, Integer> numbers = new ConcurrentHashMap<>();

        // Insert elements to map
        numbers.put("Two", 2);
        numbers.put("One", 1);
        numbers.put("Three", 3);
        System.out.println("ConcurrentMap: " + numbers);

        // Access the value of specified key
        int value = numbers.get("One");
        System.out.println("Accessed Value: " + value);

        // Remove the value of specified key
        int removedValue = numbers.remove("Two");
        System.out.println("Removed Value: " + removedValue);
    }
}

Đầu ra

ConcurrentMap: {One=1, Two=2, Three=3}
Accessed Value: 1
Removed Value: 2

Để tìm hiểu thêm về ConcurrentHashMap, hãy truy cập Java ConcurrentHashMap .









Gõ tìm kiếm nhanh...