Javaのツリーマップとは何ですか?

王林
リリース: 2024-08-30 15:45:46
オリジナル
645 人が閲覧しました

TreeMap は、Java で Map および NavigableMap インターフェイスをデプロイするために Abstract クラスとともに使用されます。マップは、キーの自然な順序に従って、またはビルダーに応じて事前に構築されたコンパレーターによってソートされます。これは、キーと値のペアを並べ替えて保存する簡単な方法です。ツリーマップによって保持されるストレージの順序は、特定のコンパレーターに関係なく、他の分類されたマップと同じである必要があります。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

次に、TreeMap のプロパティの一部を示します。

  • ユニークな要素で構成されています。
  • null キーを含めることはできません。
  • NULL 値を複数回指定できます。
  • 非同期。

宣言:

public class TreeMap<Key, Value> extends AbstractMap<Key, Value>implements NavigableMap<Key, Value>, Cloneable, Serializable
ログイン後にコピー

ここで、Key と Value はそれぞれ、このマップ内のキーとマップされた値のタイプです。

TreeMap のコンストラクター

以下は TreeMap のコンストラクターです。

  • TreeMap(): キーを自然に順序付けすることによって、新しい空のツリーマップが構築されます。
  • TreeMap( Comparator Key> comparator): コンパレーターで指定されたキーを順序付けすることによって、新しい空のツリーマップが構築されます。
  • TreeMap( MapKey,? Ext Value> m):マップ m のマッピングを使用して新しいツリーマップが構築され、キーが自然に順序付けされます。
  • TreeMap( SortedMap<Kえ、? extends Value> m):マップ m のマッピングとコンパレーターで指定されたキーの順序を使用して新しいツリーマップが構築されます。

TreeMap のメソッド

TreeMap は、さまざまな機能の実行に役立つメソッドの幅広いコレクションを提供します。それらは次のとおりです:

  • 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.

Javaのツリーマップとは何ですか?

Explanation:

  • まず、TreeMap を作成し、それに要素を追加します。
  • 要素を表示するには、イテレータを作成する必要があります。
  • イテレータを使用すると、すべてのキーとそれに対応する値が表示されます。
  • キーの値に 2500 を加算するには、put() メソッドも使用されます。

結論

Java TreeMap は、キーと値のペアを並べ替えた順序で保存するのに役立つ Red-Black ツリーの実装です。このドキュメントでは、Java TreeMap の宣言、コンストラクター、メソッド、サンプル プログラムなどの詳細について詳しく説明します。

以上がJavaのツリーマップとは何ですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!