ホームページ  >  記事  >  Java  >  Javaのツリーセット

Javaのツリーセット

王林
王林オリジナル
2024-08-30 15:07:03531ブラウズ

Java の TreeSet は、ストレージに役立つインターフェース SortedSet の主要な実装の 1 つであると考えられています。この場合、要素は、自然な順序または明示的なコンパレータに基づいた順序で設定される方法で順序付けされます。 TreeSet は AbstractSet クラスを継承し、NavigableSet インターフェイスを実装します。 HashSet に似ていますが、HashSet は順序を維持しますが、HashSet は順序を維持しません。さらに、HashSet とは異なり、TreeSet では null 要素が許可されます。 TreeSet の詳細については、次のセクションで説明します。

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

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

ツリーセットの特徴

以下は TreeSet の主な機能です。

  • 重複する値は許可されません。
  • オブジェクトは昇順に基づいて並べ替えられます。
  • 広告掲載オーダーは保存されません。それに加えて、要素はキーに基づいて並べ替えられます。
  • 異種オブジェクトの挿入は許可されません。挿入しようとすると、実行時に classCastException がスローされます。
  • ソートされた大量のデータを保存します。
  • 高速取得時間。
  • 高速アクセス。
  • NULL 要素は許可されません。
  • 追加、検索、削除などの操作には O(log n) の時間がかかります。
  • n 個の要素の出力などの操作の場合、O(n) の時間計算量が存在します。
  • 同期されていません。
  • TreeMap は要素を保存するために存在します。
  • スレッドセーフではありません。

構文:

TreeSet を作成するには、まず java.util.TreeSet パッケージをインポートします。その後、構文

を使用して TreeSet を作成します。
TreeSet<Integer> num= new TreeSet<>();

引数を何も指定せずに TreeSet を作成したため、要素は自然な順序で並べ替えられます。つまり、昇順です。

すでに述べたように、並べ替えはインターフェイス Comparator を使用してカスタマイズできます。

Java の TreeSet のコンストラクター

Java の TreeSet には 4 つのコンストラクターがあります。それらは次のとおりです:

  1. TreeSet(): 自然な順序に基づいて並べ替えられて、新しい空の TreeSet が作成されます。
  2. TreeSet(Collection c): コレクション c で言及されている要素を使用して新しい TreeSet が作成され、それらは自然な順序に基づいて並べ替えられます。
  3. TreeSet(Comparator comparator): 指定されたコンパレータに基づいて並べ替えを行って、新しい空の TreeSet が作成されます。
  4. TreeSet(SortedSet s): 新しい TreeSet は、sortedset s で言及されている要素を使用して、それと同じ順序で作成されます。

Java の TreeSet のメソッド

TreeSet では実行する必要のある機能がいくつかあります。それらが何であるかを見てみましょう。

  • add(Ee): An element e will be added to the set if it is not present in it.
  • addAll(CollectionE> c): All the elements in collection c will be added to the set.
  • E ceiling(Ee): The last element which is greater than or equal to the element in the set will be returned.
  • clear(): All the elements in the set will be removed.
  • clone(): A shallow copy will be returned for the TreeSet instance.
  • comparator(): The comparator used in the set will be returned. If natural ordering is used, null will be returned.
  • contains(Objecto): If the set contains the element o, true will be returned.
  • descendingIterator(): An iterator will be returned over the elements in descending order.
  • descendingSet(): A reverse order view will be returned for the elements present in the list.
  • first(): he first or the lowest element in the set will be returned.
  • last(): The last or the largest element in the set will be returned.
  • iterator(): n iterator will be returned over the elements in ascending order.
  • lower(Ee): The greatest element will be returned which is strictly small than the element e which is given. If there is no such element, null will be returned.
  • higher(Ee): The smallest element will be returned which is strictly high than the element e which is given. If there is no such element, null will be returned.
  • isEmpty(): True will be returned if no elements are present.
  • size(): The number of elements in the set will be returned. In other words, cardinality will be returned.
  • pollFirst(): The first or the lowest element in the set will be retrieved and removed. If there are no elements in the set, the null will be returned.
  • pollLast(): The last or the highest element in the set will be retrieved and removed. If there are no elements in the set, the null will be returned.
  • remove(Objecto): If the set contains the element o, it will be removed.
  • subSet(EfromElement, boolean fromInclusive, E toElement, boolean toInclusive): A view will be returned for the portion of the set from the range from Element to toElement.
  • subSet(EfromElement, E toElement): A view will be returned for the portion of the set from the range fromElement(inclusive) to toElement(exclusive).
  • tailSet(EfromElement):  view will be returned for the portion of the set where elements are larger than fromElement.
  • tailSet(EfromElement, boolean inclusive): A view will be returned for the portion of the set where elements are larger than fromElement. It is considered if inclusive is true.

Example of TreeSet in Java

Java program to create a tree set with the natural ordering of elements.

import java.util.SortedSet;
import java.util.TreeSet;
//class
public class TreeSetExample {
//main method
public static void main(String[] args) {
//  TreeSet <u>fam</u> creation
SortedSet<String> fam = new TreeSet<>();
// Adding new elements to a TreeSet
fam.add("Anna");
fam.add("Adam");
fam.add("Sam");
fam.add("Iza");
//print the treeset
System.out.println("Fam Set : " + fam);
// Trying to add duplicate element <u>Anna</u>
fam.add("Anna");
System.out.println("Added element Anna, Now the treeset is : " + fam);
// Trying to add  element <u>Anna</u> with lower case
fam.add("anna");
System.out.println("Added element anna, Now the treeset is  : " + fam);
}
}

Output:

Javaのツリーセット

A treeset fam is created first. Once it is created, elements are added to it and the whole treeset is printed. After this, an element ‘Anna’ which is already present in the treeset is trying to get added. Since duplicates are not allowed in TreeSet, it is not get added. After that, an element ‘anna’ which is the lower case of the already existing element ‘Anna’ is added. Since it is in lowercase, it gets added to the treeset without any problem.

Conclusion

TreeSet in Java is an implementation of the interface SortedSet that helps in storage where the elements are arranged in natural ordering or based on the comparator. Several aspects of TreeSet is discussed in this document.

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

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。