Rumah  >  Artikel  >  Java  >  Apakah TreeMap dalam Java?

Apakah TreeMap dalam Java?

王林
王林asal
2024-08-30 15:45:46646semak imbas

TreeMap digunakan dengan Kelas Abstrak untuk menggunakan antara muka Map dan NavigableMap dalam Java. Peta diisih mengikut urutan semula jadi kekuncinya atau oleh pembanding pra-bina yang bergantung pada pembina. Ini ialah cara mudah untuk mengisih dan menyimpan pasangan nilai kunci. Susunan storan yang dikekalkan oleh peta pokok mestilah sama seperti mana-mana peta terperingkat lain, tanpa mengira pembanding tertentu.

Mulakan Kursus Pembangunan Perisian Percuma Anda

Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain

Berikut ialah beberapa sifat TreeMap.

  • Ia terdiri daripada elemen unik.
  • Ia tidak boleh mempunyai kunci null.
  • Boleh mempunyai nilai nol beberapa kali.
  • Tidak disegerakkan.

Pengisytiharan:

public class TreeMap<Key, Value> extends AbstractMap<Key, Value>implements NavigableMap<Key, Value>, Cloneable, Serializable

Di sini, Kunci dan Nilai ialah jenis kunci dan nilai yang dipetakan dalam peta ini, masing-masing.

Pembina TreeMap

Berikut ialah pembina TreeMap.

  • TreeMap(): Peta pokok baharu dan kosong akan dibina dengan memesan kekunci secara semula jadi.
  • Peta Pokok( Pembanding Key> pembanding): Peta pokok baharu dan kosong akan dibina dengan memesan kekunci yang dinyatakan dalam pembanding.
  • Peta Pokok( PetaKey,? Ext Value>  m): Peta pokok baharu akan dibina dengan pemetaan dalam peta m dan susunan kekunci secara semula jadi.
  • Peta Pokok( Peta Isih<Key,? meluas Value>  m): Peta pokok baharu akan dibina dengan pemetaan dalam peta m dan memesan kekunci yang dinyatakan dalam pembanding.

Kaedah TreeMap

TreeMap menawarkan pelbagai koleksi kaedah yang membantu dalam melaksanakan fungsi yang berbeza. Mereka ialah:

  • clear(): All the mapping in the map will be removed.
  • clone(): A shallow copy will be returned for the TreeMap instance.
  • containsKey(Objectk): If there is mapping available for the specified key, true will be returned.
  • containsValue(Objectv): If there is mapping available for one or more keys for the value v, true will be returned.
  • ceilingKey(Kkey): Least key which is larger than or equal to the specified key will be returned. If there is no key, then null will be returned.
  • ceilingEntry(Kkey): Key-value pair for the least key, which is larger than or equal to the specified key will be returned. If there is no key, then null will be returned.
  • firstKey(): First or last key in the map will be returned.
  • firstEntry(): Key-value pair for the least or first key in the map will be returned. If there is no key, then null will be returned.
  • floorKey(Kkey): The Largest key, which is less than or equal to the specified key, will be returned. If there is no key, then null will be returned.
  • floorEntry(Kkey): Key-value pair for the largest key, which is less than or equal to the specified key, will be returned. If there is no key, then null will be returned.
  • lastKey(): Highest or last key in the map will be returned.
  • lastEntry(): Key-value pair for the largest key in the map will be returned. If there is no key, then null will be returned.
  • lowerKey(Kkey): The Largest key, which is strictly smaller than the specified key, will be returned. If there is no key, then null will be returned.
  • lowerEntry(Kkey): Key-value pair for the largest key, which is strictly smaller than the specified key, will be returned. If there is no key, then null will be returned.
  • remove(Objectk): Mapping for the specified key in the map will be removed.
  • size(): Count of key-value pairs in the map will be returned.
  • higherEntry(Kkey): Key-value pair for the smallest key, which is strictly larger than the specified key, will be returned. If there is no key, then null will be returned.
  • higherKey(Kkey): The Smallest key, which is strictly higher than the specified key, will be returned. If there is no key, then null will be returned.
  • descendingMap(): Reverse order will be returned for the mappings.
  • entrySet(): Set view will be returned for the mappings.
  • get(Objectk): The value of the specified key will be returned. If the key does not contain any mapping, then null will be returned.
  • keySet(): Set view will be returned for the keys.
  • navigableKeySet(): NavigableSet view will be returned for the keys.
  • pollFirstEntry(): Key-value pair for the least or first key in the map will be removed and returned. If the map is empty, then null will be returned.
  • pollLastEntry(): Key-value pairs for the greatest key in the map will be removed and returned. If the map is empty, then null will be returned.
  • values(): Collection view will be returned for the values in the map.

Example to Implement TreeMap in Java

Now, let us see a sample program to create a treemap and add elements to it.

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapExample {
public static void main(String args[]) {
// Tree Map creation
TreeMap tmap = new TreeMap();
// Add elements to the treemap tmap
tmap.put("Anna Jeshua", new Double(3459.70));
tmap.put("Annamu Jeshua", new Double(321.56));
tmap.put("Izanorah Denan", new Double(1234.87));
tmap.put("Adam Jeshua", new Double(89.35));
tmap.put("Anabeth Jeshua", new Double(-20.98));
// Retrieve the entry set of treemap
Set set = tmap.entrySet();
// Create an iterator itr
Iterator itr = set.iterator();
// Display the elements in treemap using while loop
while(itr.hasNext()) {
Map.Entry mp = (Map.Entry)itr.next();
System.out.print(" Key : " + mp.getKey() + ";");
System.out.println(" Value : "+ mp.getValue());
}
System.out.println();
// Add 2500 to Anabeth's value
double val = ((Double)tmap.get("Anabeth Jeshua")).doubleValue();
tmap.put("Anabeth Jeshua", new Double(val + 2500));
System.out.println("Anabeth's new value: " + tmap.get("Anabeth Jeshua"));
} }

Output:

Keys and corresponding values of the TreeMap will be displayed on executing the code.

Apakah TreeMap dalam Java?

Explanation:

  • Pertama sekali, buat TreeMap dan tambahkan elemen padanya.
  • Untuk memaparkan elemen, iterator perlu dibuat.
  • Menggunakan iterator, semua kekunci dan nilai sepadannya dipaparkan.
  • Untuk menambah 2500 pada nilai kunci, kaedah put() juga digunakan.

Kesimpulan

Java TreeMap ialah pelaksanaan pokok Merah-Hitam yang membantu dalam menyimpan pasangan nilai kunci dalam tertib diisih. Dalam dokumen ini, beberapa butiran seperti pengisytiharan, pembina, kaedah dan program contoh Java TreeMap dibincangkan secara terperinci.

Atas ialah kandungan terperinci Apakah TreeMap dalam Java?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Alias ​​JawaArtikel seterusnya:Alias ​​Jawa