Java : TreeSet (ソートされたセット) - API使用例
TreeSet (Java SE 23 & JDK 23) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
APIドキュメントのおともにどうぞ。
概要
TreeMapに基づくNavigableSet実装です。 要素の順序付けは、自然順序付けを使って行われるか、セット構築時に提供されるComparatorを使って行われます。そのどちらになるかは、使用するコンストラクタによって決まります。
TreeSet は、自然順序、もしくは指定した Comparator でソートされる Set です。
関連記事:Set(セット)の基本
final var set = new TreeSet<String>();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("x")); // false
final var treeSet = new TreeSet<String>();
treeSet.add("ccc");
treeSet.add("ddd");
treeSet.add("aaa");
treeSet.add("bbb");
System.out.println(treeSet); // [aaa, bbb, ccc, ddd]
final var hashSet = new HashSet<String>();
hashSet.add("ccc");
hashSet.add("ddd");
hashSet.add("aaa");
hashSet.add("bbb");
System.out.println(hashSet); // [aaa, ccc, bbb, ddd]
コンストラクタ
TreeSet ()
要素の自然順序付けに従ってソートされた、新しい空のツリー・セットを作成します。
final var set = new TreeSet<String>();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true
set.add("b");
set.add("a");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
final var set = new TreeSet<Integer>();
set.add(100);
set.add(999);
set.add(-200);
System.out.println(set); // [-200, 100, 999]
TreeSet (Collection<? extends E> c)
指定されたコレクション内の要素を持ち、その要素の自然順序付けに従ってソートされた新しいツリー・セットを作成します。
final var c = List.of("b", "a", "c");
final var set = new TreeSet<>(c);
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
TreeSet (Comparator<? super E> comparator)
指定されたコンパレータに従ってソートされた、新しい空のツリー・セットを作成します。
final var src = List.of("bbb", "aaa", "ccc");
final var set1 = new TreeSet<String>();
set1.addAll(src);
System.out.println(set1); // [aaa, bbb, ccc]
final var set2 = new TreeSet<String>(Comparator.reverseOrder());
set2.addAll(src);
System.out.println(set2); // [ccc, bbb, aaa]
TreeSet (SortedSet<E> s)
指定されたソート・セットと同じ要素を持ち、同じ順序付けを使用する新しいツリー・セットを作成します。
final var s = new TreeSet<String>(Comparator.reverseOrder());
s.add("a");
s.add("b");
s.add("c");
System.out.println(s); // [c, b, a]
final var set = new TreeSet<>(s);
System.out.println(set); // [c, b, a]
System.out.println(set.size()); // 3
メソッド
boolean add (E e)
指定された要素がセットの要素として存在しない場合に、その要素をセットに追加します。
final var set = new TreeSet<String>();
System.out.println(set); // []
System.out.println(set.add("a")); // true
System.out.println(set); // [a]
System.out.println(set.add("b")); // true
System.out.println(set); // [a, b]
System.out.println(set.add("c")); // true
System.out.println(set); // [a, b, c]
System.out.println(set.add("a")); // false
System.out.println(set); // [a, b, c]
boolean addAll (Collection<? extends E> c)
指定されたコレクション内のすべての要素をセットに追加します。
final var src = Set.of("a", "b", "c");
{
final var set = new TreeSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(Set.of("d", "e", "f"))); // true
System.out.println(set); // [a, b, c, d, e, f]
}
{
final var set = new TreeSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(Set.of("a", "b", "c"))); // false
System.out.println(set); // [a, b, c]
}
{
final var set = new TreeSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(Set.of("a", "c", "e", "f"))); // true
System.out.println(set); // [a, b, c, e, f]
}
void addFirst (E e)
UnsupportedOperationExceptionをスローします。
final var set = new TreeSet<>();
System.out.println(set); // []
try {
set.addFirst("aaa");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
void addLast (E e)
UnsupportedOperationExceptionをスローします。
final var set = new TreeSet<>();
System.out.println(set); // []
try {
set.addLast("aaa");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
E ceiling (E e)
このセット内で、指定された要素と等しいかそれよりも大きい要素の中で最小のものを返します。そのような要素が存在しない場合はnullを返します。
final var set = new TreeSet<String>();
set.add("b");
set.add("d");
set.add("f");
System.out.println(set); // [b, d, f]
System.out.println(set.ceiling("a")); // b
System.out.println(set.ceiling("b")); // b
System.out.println(set.ceiling("c")); // d
System.out.println(set.ceiling("d")); // d
System.out.println(set.ceiling("e")); // f
System.out.println(set.ceiling("f")); // f
System.out.println(set.ceiling("g")); // null
void clear ()
すべての要素をセットから削除します。
final var set = new TreeSet<String>();
System.out.println(set); // []
set.add("a");
System.out.println(set); // [a]
set.add("b");
System.out.println(set); // [a, b]
set.clear();
System.out.println(set); // []
Object clone ()
TreeSetのインスタンスのシャロー・コピーを返します。
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
final var cloned = set.clone();
System.out.println(cloned); // [a, b, c]
System.out.println(cloned.getClass()); // class java.util.TreeSet
Comparator<? super E> comparator ()
このセットの要素の順序付けに使用されるコンパレータを返します。このセットが要素の「自然順序付け」を使用する場合はnullを返します。
final var set = new TreeSet<String>();
System.out.println(set); // []
final var comparator = set.comparator();
System.out.println(comparator); // null
final var set = new TreeSet<String>(Comparator.reverseOrder());
System.out.println(set); // []
final var comparator = set.comparator();
System.out.println(Objects.equals(comparator, Comparator.reverseOrder())); // true
final var set = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
System.out.println(set); // []
final var comparator = set.comparator();
System.out.println(Objects.equals(comparator, String.CASE_INSENSITIVE_ORDER)); // true
boolean contains (Object o)
指定された要素がこのセットに含まれている場合にtrueを返します。
final var set = new TreeSet<String>();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]
System.out.println(set.contains("")); // false
System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("c")); // true
System.out.println(set.contains("X")); // false
System.out.println(set.contains("abc")); // false
Iterator<E> descendingIterator ()
このセットの要素のイテレータを降順で返します。
final var set = new TreeSet<String>();
set.add("aaa");
set.add("bbb");
set.add("ccc");
System.out.println(set); // [aaa, bbb, ccc]
final var iterator = set.descendingIterator();
System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(s -> {
System.out.println(s);
});
// 結果
// ↓
//-- forEachRemaining --
//ccc
//bbb
//aaa
NavigableSet<E> descendingSet ()
このセットに含まれる要素の逆順のビューを返します。
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
final var descSet = set.descendingSet();
System.out.println(descSet); // [c, b, a]
set.add("d");
System.out.println(set); // [a, b, c, d]
System.out.println(descSet); // [d, c, b, a]
descSet.remove("b");
System.out.println(set); // [a, c, d]
System.out.println(descSet); // [d, c, a]
E first ()
セット内に現在ある最初(下端)の要素を返します。
final var set = new TreeSet<String>();
System.out.println(set); // []
set.add("b");
System.out.println(set); // [b]
System.out.println(set.first()); // b
set.add("c");
System.out.println(set); // [b, c]
System.out.println(set.first()); // b
set.add("a");
System.out.println(set); // [a, b, c]
System.out.println(set.first()); // a
final var set = new TreeSet<String>();
System.out.println(set); // []
try {
set.first();
} catch (NoSuchElementException e) {
System.out.println(e);
}
// 結果
// ↓
//java.util.NoSuchElementException
E floor (E e)
このセット内で、指定された要素と等しいかそれよりも小さい要素の中で最大のものを返します。そのような要素が存在しない場合はnullを返します。
final var set = new TreeSet<String>();
set.add("b");
set.add("d");
set.add("f");
System.out.println(set); // [b, d, f]
System.out.println(set.floor("a")); // null
System.out.println(set.floor("b")); // b
System.out.println(set.floor("c")); // b
System.out.println(set.floor("d")); // d
System.out.println(set.floor("e")); // d
System.out.println(set.floor("f")); // f
System.out.println(set.floor("g")); // f
SortedSet<E> headSet (E toElement)
このセットの要素がtoElementより厳密に小さい部分のビューを返します。
final var set = new TreeSet<String>();
set.add("x");
set.add("y");
set.add("z");
System.out.println(set); // [x, y, z]
final var headSet = set.headSet("z");
System.out.println(headSet); // [x, y]
set.add("a");
System.out.println(set); // [a, x, y, z]
System.out.println(headSet); // [a, x, y]
headSet.remove("x");
System.out.println(set); // [a, y, z]
System.out.println(headSet); // [a, y]
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.headSet("a")); // []
System.out.println(set.headSet("b")); // [a]
System.out.println(set.headSet("c")); // [a, b]
System.out.println(set.headSet("d")); // [a, b, c]
NavigableSet<E> headSet (E toElement, boolean inclusive)
このセットのtoElementよりも小さい要素(inclusiveがtrueの場合はそれよりも小さいかそれと等しい要素)を含む部分のビューを返します。
final var set = new TreeSet<String>();
set.add("x");
set.add("y");
set.add("z");
System.out.println(set); // [x, y, z]
final var headSet = set.headSet("y", true);
System.out.println(headSet); // [x, y]
set.add("a");
System.out.println(set); // [a, x, y, z]
System.out.println(headSet); // [a, x, y]
headSet.remove("x");
System.out.println(set); // [a, y, z]
System.out.println(headSet); // [a, y]
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.headSet("a")); // []
System.out.println(set.headSet("a", true)); // [a]
System.out.println(set.headSet("b")); // [a]
System.out.println(set.headSet("b", true)); // [a, b]
System.out.println(set.headSet("c")); // [a, b]
System.out.println(set.headSet("c", true)); // [a, b, c]
System.out.println(set.headSet("d")); // [a, b, c]
System.out.println(set.headSet("d", true)); // [a, b, c]
E higher (E e)
このセット内で、指定された要素よりも確実に大きい要素の中で最小のものを返します。そのような要素が存在しない場合はnullを返します。
final var set = new TreeSet<String>();
set.add("b");
set.add("d");
set.add("f");
System.out.println(set); // [b, d, f]
System.out.println(set.higher("a")); // b
System.out.println(set.higher("b")); // d
System.out.println(set.higher("c")); // d
System.out.println(set.higher("d")); // f
System.out.println(set.higher("e")); // f
System.out.println(set.higher("f")); // null
System.out.println(set.higher("g")); // null
boolean isEmpty ()
このセットに要素が1つも含まれていない場合にtrueを返します。
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.isEmpty()); // false
set.clear();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true
Iterator<E> iterator ()
このセットの要素のイテレータを昇順で返します。
final var set = new TreeSet<String>();
set.add("aaa");
set.add("bbb");
set.add("ccc");
System.out.println(set); // [aaa, bbb, ccc]
final var iterator = set.iterator();
System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(s -> {
System.out.println(s);
});
// 結果
// ↓
//-- forEachRemaining --
//aaa
//bbb
//ccc
E last ()
セット内に現在ある最後(上端)の要素を返します。
final var set = new TreeSet<String>();
System.out.println(set); // []
set.add("b");
System.out.println(set); // [b]
System.out.println(set.last()); // b
set.add("a");
System.out.println(set); // [a, b]
System.out.println(set.last()); // b
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.last()); // c
final var set = new TreeSet<String>();
System.out.println(set); // []
try {
set.last();
} catch (NoSuchElementException e) {
System.out.println(e);
}
// 結果
// ↓
//java.util.NoSuchElementException
E lower (E e)
このセット内で、指定された要素よりも確実に小さい要素の中で最大のものを返します。そのような要素が存在しない場合はnullを返します。
final var set = new TreeSet<String>();
set.add("b");
set.add("d");
set.add("f");
System.out.println(set); // [b, d, f]
System.out.println(set.lower("a")); // null
System.out.println(set.lower("b")); // null
System.out.println(set.lower("c")); // b
System.out.println(set.lower("d")); // b
System.out.println(set.lower("e")); // d
System.out.println(set.lower("f")); // d
System.out.println(set.lower("g")); // f
E pollFirst ()
最初の(最低)要素を取得して削除するか、このセットが空の(オプションの操作)の場合はnullを返します。
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.pollFirst()); // a
System.out.println(set); // [b, c]
System.out.println(set.pollFirst()); // b
System.out.println(set); // [c]
System.out.println(set.pollFirst()); // c
System.out.println(set); // []
System.out.println(set.pollFirst()); // null
E pollLast ()
最後の(最高)要素を取得して削除するか、このセットが空の(オプションの操作)の場合はnullを返します。
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.pollLast()); // c
System.out.println(set); // [a, b]
System.out.println(set.pollLast()); // b
System.out.println(set); // [a]
System.out.println(set.pollLast()); // a
System.out.println(set); // []
System.out.println(set.pollLast()); // null
boolean remove (Object o)
指定された要素がこのセットに存在する場合に、要素をセットから削除します。
final var src = Set.of("a", "b", "c");
{
final var set = new TreeSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("")); // false
System.out.println(set); // [a, b, c]
}
{
final var set = new TreeSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("a")); // true
System.out.println(set); // [b, c]
}
{
final var set = new TreeSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("b")); // true
System.out.println(set); // [a, c]
}
{
final var set = new TreeSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("X")); // false
System.out.println(set); // [a, b, c]
}
int size ()
セット内の要素数(そのカーディナリティ)を返します。
final var set1 = new TreeSet<String>();
System.out.println(set1); // []
System.out.println(set1.size()); // 0
final var set2 = new TreeSet<String>();
Collections.addAll(set2, "a", "b", "c");
System.out.println(set2); // [a, b, c]
System.out.println(set2.size()); // 3
Spliterator<E> spliterator ()
このセット内の要素に対する遅延バインディングおよびフェイルファスト Spliteratorを作成します。
final var set = new TreeSet<String>();
set.add("aaa");
set.add("bbb");
set.add("ccc");
System.out.println(set); // [aaa, bbb, ccc]
final var spliterator = set.spliterator();
System.out.println("-- forEachRemaining --");
spliterator.forEachRemaining(s -> {
System.out.println(s);
});
// 結果
// ↓
//-- forEachRemaining --
//aaa
//bbb
//ccc
NavigableSet<E> subSet (E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
このセットのfromElement - toElementの要素範囲を持つ部分のビューを返します。
final var set = new TreeSet<String>();
set.add("x");
set.add("y");
set.add("z");
System.out.println(set); // [x, y, z]
final var subSet = set.subSet("a", true, "y", true);
System.out.println(subSet); // [x, y]
set.add("a");
System.out.println(set); // [a, x, y, z]
System.out.println(subSet); // [a, x, y]
subSet.remove("x");
System.out.println(set); // [a, y, z]
System.out.println(subSet); // [a, y]
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.subSet("a", true, "c", true)); // [a, b, c]
System.out.println(set.subSet("a", true, "c", false)); // [a, b]
System.out.println(set.subSet("a", false, "c", true)); // [b, c]
System.out.println(set.subSet("a", false, "c", false)); // [b]
System.out.println(set.subSet("a", true, "a", false)); // []
System.out.println(set.subSet("a", true, "b", false)); // [a]
System.out.println(set.subSet("a", true, "c", false)); // [a, b]
System.out.println(set.subSet("a", true, "d", false)); // [a, b, c]
System.out.println(set.subSet("a", true, "d", false)); // [a, b, c]
System.out.println(set.subSet("b", true, "d", false)); // [b, c]
System.out.println(set.subSet("c", true, "d", false)); // [c]
System.out.println(set.subSet("d", true, "d", false)); // []
SortedSet<E> subSet (E fromElement, E toElement)
このセットの要素の範囲がfromElement、包含的、排他的、toElementである部分のビューを返します。
final var set = new TreeSet<String>();
set.add("x");
set.add("y");
set.add("z");
System.out.println(set); // [x, y, z]
final var subSet = set.subSet("a", "z");
System.out.println(subSet); // [x, y]
set.add("a");
System.out.println(set); // [a, x, y, z]
System.out.println(subSet); // [a, x, y]
subSet.remove("x");
System.out.println(set); // [a, y, z]
System.out.println(subSet); // [a, y]
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.subSet("a", "a")); // []
System.out.println(set.subSet("a", "b")); // [a]
System.out.println(set.subSet("a", "c")); // [a, b]
System.out.println(set.subSet("a", "d")); // [a, b, c]
System.out.println(set.subSet("a", "d")); // [a, b, c]
System.out.println(set.subSet("b", "d")); // [b, c]
System.out.println(set.subSet("c", "d")); // [c]
System.out.println(set.subSet("d", "d")); // []
SortedSet<E> tailSet (E fromElement)
要素がfromElement以上である、このセットの一部のビューを返します。
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
final var tailSet = set.tailSet("b");
System.out.println(tailSet); // [b, c]
set.add("d");
System.out.println(set); // [a, b, c, d]
System.out.println(tailSet); // [b, c, d]
tailSet.remove("b");
System.out.println(set); // [a, c, d]
System.out.println(tailSet); // [c, d]
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.tailSet("a")); // [a, b, c]
System.out.println(set.tailSet("b")); // [b, c]
System.out.println(set.tailSet("c")); // [c]
System.out.println(set.tailSet("d")); // []
NavigableSet<E> tailSet (E fromElement, boolean inclusive)
このセットのfromElementよりも大きい要素(inclusiveがtrueの場合はそれよりも大きいかそれと等しい要素)を含む部分のビューを返します。
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
final var tailSet = set.tailSet("a", false);
System.out.println(tailSet); // [b, c]
set.add("d");
System.out.println(set); // [a, b, c, d]
System.out.println(tailSet); // [b, c, d]
tailSet.remove("b");
System.out.println(set); // [a, c, d]
System.out.println(tailSet); // [c, d]
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.tailSet("a")); // [a, b, c]
System.out.println(set.tailSet("a", false)); // [b, c]
System.out.println(set.tailSet("b")); // [b, c]
System.out.println(set.tailSet("b", false)); // [c]
System.out.println(set.tailSet("c")); // [c]
System.out.println(set.tailSet("c", false)); // []
AbstractSetで宣言されたメソッド
equals, hashCode, removeAll
「Java API 使用例 : AbstractSet」をご参照ください。
AbstractCollectionで宣言されたメソッド
containsAll, retainAll, toArray, toArray, toString
「Java API 使用例 : AbstractCollection」をご参照ください。
Collectionで宣言されたメソッド
parallelStream, removeIf, stream, toArray
「Java API 使用例 : Collection」をご参照ください。
Iterableで宣言されたメソッド
forEach
「Java API 使用例 : Iterable」をご参照ください。
NavigableSetで宣言されたメソッド
removeFirst, removeLast, reversed
「Java API 使用例 : NavigableSet」をご参照ください。
Setで宣言されたメソッド
containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray
「Java API 使用例 : Set」をご参照ください。
SortedSetで宣言されたメソッド
getFirst, getLast
「Java API 使用例 : SortedSet」をご参照ください。
関連記事
- List(リスト)の基本
- Map(マップ)の基本
- Set(セット)の基本
- List の初期化方法いろいろ
- Map の初期化方法いろいろ
- Set の初期化方法いろいろ
- 配列 vs. List
- 配列からListへの変換
- List から配列への変換
- API 使用例
- Collection (コレクション)
- Collections (コレクション操作)
- Comparable
- Comparator
- Iterator
- List (リスト)
- Map (マップ)
- Map.Entry (キーと値のペア)
- Queue (キュー)
- AbstractQueue
- ArrayBlockingQueue (ブロッキング・配列キュー)
- ArrayDeque (両端キュー)
- BlockingDeque (ブロッキング・両端キュー)
- BlockingQueue (ブロッキング・キュー)
- ConcurrentLinkedDeque (並列処理用・両端キュー)
- ConcurrentLinkedQueue (並列処理用キュー)
- Deque (両端キュー)
- LinkedBlockingDeque (ブロッキング・リンク両端キュー)
- LinkedBlockingQueue (ブロッキング・リンクキュー)
- LinkedList (二重リンク・リスト)
- PriorityBlockingQueue (ブロッキング・優先度付きキュー)
- PriorityQueue (優先度付きキュー)
- RandomAccess
- Set (セット)
- Spliterator