Dalam tutorial ini, kita akan belajar mengenai antara muka Peta Java dan kaedahnya.
Antara Map
muka kerangka koleksi Java menyediakan fungsi struktur data peta.
Kerja Peta
Di Jawa, elemen Map
disimpan dalam pasangan kunci / nilai . Kunci adalah nilai unik yang berkaitan dengan Nilai individu .
Peta tidak boleh mengandungi kunci pendua. Dan, setiap kunci dikaitkan dengan satu nilai.
Kita boleh mengakses dan mengubah nilai menggunakan kunci yang berkaitan dengannya.
Dalam rajah di atas, kami mempunyai nilai: Amerika Syarikat, Brazil, dan Sepanyol. Dan kami mempunyai kunci yang sesuai: kami, br, dan es.
Sekarang, kita dapat mengakses nilai tersebut dengan menggunakan kekunci yang sesuai.
Nota: Antara Map
muka mengekalkan 3 set berbeza:
- set kunci
- himpunan nilai
- himpunan persatuan kunci / nilai (pemetaan).
Oleh itu kita dapat mengakses kunci, nilai, dan persatuan secara individu.
Kelas yang melaksanakan Peta
Oleh kerana Map
merupakan antara muka, kita tidak dapat membuat objek daripadanya.
Untuk menggunakan fungsi Map
antara muka, kita dapat menggunakan kelas berikut:
- Peta Hash
- Peta Enum
- LinkedHashMap
- Peta Lemah
- Peta Pokok
Kelas-kelas ini ditentukan dalam kerangka koleksi dan melaksanakan Map
antara muka.

Antara muka yang memanjangkan Peta
Yang Map
antara muka juga diperluaskan oleh subinterfaces ini:
- Peta Disusun
- Peta Navigasi
- Peta Serentak

Bagaimana menggunakan Peta?
Di Jawa, kita mesti mengimport java.util.Map
paket untuk digunakan Map
. Sebaik sahaja kami mengimport pakej, inilah cara kami dapat membuat peta.
// Map implementation using HashMap Map numbers = new HashMap();
Dalam kod di atas, kami telah membuat Map
nombor yang dinamakan. Kami telah menggunakan HashMap
kelas untuk melaksanakan Map
antara muka.
Di sini,
- Kunci - pengecam unik yang digunakan untuk mengaitkan setiap elemen (nilai) dalam peta
- Nilai - elemen yang berkaitan dengan kunci dalam peta
Kaedah Peta
Yang Map
antara muka termasuk semua kaedah daripada Collection
antara muka. Ini kerana Collection
antara muka super Map
.
Besides methods available in the Collection
interface, the Map
interface also includes the following methods:
- 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 () - Mengembalikan sekumpulan semua pemetaan kunci / nilai yang terdapat dalam peta.
Pelaksanaan Antaramuka Peta
1. Melaksanakan Kelas HashMap
import java.util.Map; import java.util.HashMap; class Main ( public static void main(String() args) ( // Creating a map using the HashMap Map 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); ) )
Pengeluaran
Peta: (Satu = 1, Dua = 2) Kekunci: (Satu, Dua) Nilai: (1, 2) Entri: (Satu = 1, Dua = 2) Nilai yang Dihapus: 2
Untuk mengetahui lebih lanjut mengenai HashMap
, lawati Java HashMap.
2. Melaksanakan Kelas TreeMap
import java.util.Map; import java.util.TreeMap; class Main ( public static void main(String() args) ( // Creating Map using TreeMap Map 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); ) )
Pengeluaran
Peta menggunakan TreeMap: (Pertama = 1, Kedua = 2) Peta Baru: (Pertama = 11, Kedua = 22) Nilai yang dikeluarkan: 11
Untuk mengetahui lebih lanjut mengenai TreeMap
, lawati Java TreeMap.