Added in API level 1

TreeSet

open class TreeSet<E : Any!> : AbstractSet<E>, NavigableSet<E>, Cloneable, Serializable, MutableSet<E>
kotlin.Any
   ↳ java.util.AbstractCollection<E>
   ↳ java.util.AbstractSet<E>
   ↳ java.util.TreeSet

A NavigableSet implementation based on a TreeMap. The elements are ordered using their ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.

Note that this implementation is not synchronized. If multiple threads access a tree set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using the Collections.synchronizedSortedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the set:

SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));

The iterators returned by this class's iterator method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

The #addFirst and #addLast methods of this class throw UnsupportedOperationException. The encounter order of elements is determined by the comparison method; therefore, explicit positioning is not supported.

This class is a member of the Java Collections Framework.

Summary

Public constructors

Constructs a new, empty tree set, sorted according to the natural ordering of its elements.

TreeSet(comparator: Comparator<in E>!)

Constructs a new, empty tree set, sorted according to the specified comparator.

Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements.

Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.

Public methods
open Boolean
add(element: E)

Adds the specified element to this set if it is not already present.

open Boolean
addAll(elements: Collection<E>)

Adds all of the elements in the specified collection to this set.

open Unit
addFirst(e: E)

Throws UnsupportedOperationException.

open Unit
addLast(e: E)

Throws UnsupportedOperationException.

open E
ceiling(e: E)

open Unit

Removes all of the elements from this set.

open Any

Returns a shallow copy of this TreeSet instance.

open Comparator<in E>!

open Boolean
contains(element: E)

Returns true if this set contains the specified element.

open Boolean
contains(element: E)

Returns true if this set contains the specified element.

open MutableIterator<E>!

Returns an iterator over the elements in this set in descending order.

open NavigableSet<E>!

open E

open E
floor(e: E)

open NavigableSet<E>!
headSet(toElement: E, inclusive: Boolean)

open SortedSet<E>!
headSet(toElement: E)

open E
higher(e: E)

open Boolean

Returns true if this set contains no elements.

open MutableIterator<E>

Returns an iterator over the elements in this set in ascending order.

open E

open E
lower(e: E)

open E

open E

open Boolean
remove(element: E)

Removes the specified element from this set if it is present.

open Boolean
remove(element: E)

Removes the specified element from this set if it is present.

open Spliterator<E>

Creates a late-binding and fail-fast Spliterator over the elements in this set.

open NavigableSet<E>!
subSet(fromElement: E, fromInclusive: Boolean, toElement: E, toInclusive: Boolean)

open SortedSet<E>!
subSet(fromElement: E, toElement: E)

open NavigableSet<E>!
tailSet(fromElement: E, inclusive: Boolean)

open SortedSet<E>!
tailSet(fromElement: E)

Inherited functions
Properties
open Int

Returns the number of elements in this set (its cardinality).

Inherited properties

Public constructors

TreeSet

Added in API level 1
TreeSet()

Constructs a new, empty tree set, sorted according to the natural ordering of its elements. All elements inserted into the set must implement the Comparable interface. Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set. If the user attempts to add an element to the set that violates this constraint (for example, the user attempts to add a string element to a set whose elements are integers), the add call will throw a ClassCastException.

TreeSet

Added in API level 1
TreeSet(comparator: Comparator<in E>!)

Constructs a new, empty tree set, sorted according to the specified comparator. All elements inserted into the set must be mutually comparable by the specified comparator: comparator.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the set. If the user attempts to add an element to the set that violates this constraint, the add call will throw a ClassCastException.

Parameters
comparator Comparator<in E>!: the comparator that will be used to order this set. If null, the ordering of the elements will be used.

TreeSet

Added in API level 1
TreeSet(c: MutableCollection<out E>!)

Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements. All elements inserted into the set must implement the Comparable interface. Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set.

Parameters
c MutableCollection<out E>!: collection whose elements will comprise the new set
Exceptions
java.lang.ClassCastException if the elements in c are not Comparable, or are not mutually comparable
java.lang.NullPointerException if the specified collection is null

TreeSet

Added in API level 1
TreeSet(s: SortedSet<E>!)

Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.

Parameters
s SortedSet<E>!: sorted set whose elements will comprise the new set
Exceptions
java.lang.NullPointerException if the specified sorted set is null

Public methods

add

Added in API level 1
open fun add(element: E): Boolean

Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if the set contains no element e2 such that Objects.equals(e, e2). If this set already contains the element, the call leaves the set unchanged and returns false.

Parameters
e element to be added to this set
Return
Boolean true if this set did not already contain the specified element
Exceptions
java.lang.UnsupportedOperationException if the add operation is not supported by this set
java.lang.ClassCastException if the specified object cannot be compared with the elements currently in this set
java.lang.NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this set
java.lang.IllegalStateException if the element cannot be added at this time due to insertion restrictions

addAll

Added in API level 1
open fun addAll(elements: Collection<E>): Boolean

Adds all of the elements in the specified collection to this set.

Parameters
c collection containing elements to be added to this set
Return
Boolean true if this set changed as a result of the call
Exceptions
java.lang.UnsupportedOperationException if the addAll operation is not supported by this set
java.lang.ClassCastException if the elements provided cannot be compared with the elements currently in the set
java.lang.NullPointerException if the specified collection is null or if any element is null and this set uses natural ordering, or its comparator does not permit null elements
java.lang.IllegalArgumentException if some property of an element of the specified collection prevents it from being added to this set
java.lang.IllegalStateException if not all the elements can be added at this time due to insertion restrictions

addFirst

Added in API level 35
open fun addFirst(e: E): Unit

Throws UnsupportedOperationException. The encounter order induced by this set's comparison method determines the position of elements, so explicit positioning is not supported.

Parameters
e E: the element to be added
Exceptions
java.lang.NullPointerException if the specified element is null and this collection does not permit null elements
java.lang.UnsupportedOperationException always

addLast

Added in API level 35
open fun addLast(e: E): Unit

Throws UnsupportedOperationException. The encounter order induced by this set's comparison method determines the position of elements, so explicit positioning is not supported.

Parameters
e E: the element to be added.
Exceptions
java.lang.NullPointerException if the specified element is null and this collection does not permit null elements
java.lang.UnsupportedOperationException always

ceiling

Added in API level 9
open fun ceiling(e: E): E
Parameters
e E: the value to match
Return
E the least element greater than or equal to e, or null if there is no such element
Exceptions
java.lang.ClassCastException if the specified element cannot be compared with the elements currently in the set
java.lang.NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

clear

Added in API level 1
open fun clear(): Unit

Removes all of the elements from this set. The set will be empty after this call returns.

Exceptions
java.lang.UnsupportedOperationException if the clear method is not supported by this set

clone

Added in API level 1
open fun clone(): Any

Returns a shallow copy of this TreeSet instance. (The elements themselves are not cloned.)

Return
Any a shallow copy of this set
Exceptions
java.lang.CloneNotSupportedException if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.

comparator

Added in API level 1
open fun comparator(): Comparator<in E>!
Return
Comparator<in E>! the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements

contains

Added in API level 1
open fun contains(element: E): Boolean

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that Objects.equals(o, e).

Parameters
o object to be checked for containment in this set
Return
Boolean true if this set contains the specified element
Exceptions
java.lang.ClassCastException if the specified object cannot be compared with the elements currently in the set
java.lang.NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

contains

Added in API level 1
open fun contains(element: E): Boolean

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that Objects.equals(o, e).

Parameters
o object to be checked for containment in this set
Return
Boolean true if this set contains the specified element
Exceptions
java.lang.ClassCastException if the specified object cannot be compared with the elements currently in the set
java.lang.NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

descendingIterator

Added in API level 9
open fun descendingIterator(): MutableIterator<E>!

Returns an iterator over the elements in this set in descending order.

Return
MutableIterator<E>! an iterator over the elements in this set in descending order

descendingSet

Added in API level 9
open fun descendingSet(): NavigableSet<E>!
Return
NavigableSet<E>! a reverse order view of this set

first

Added in API level 1
open fun first(): E
Return
E the first (lowest) element currently in this set
Exceptions
java.util.NoSuchElementException if this set is empty

floor

Added in API level 9
open fun floor(e: E): E
Parameters
e E: the value to match
Return
E the greatest element less than or equal to e, or null if there is no such element
Exceptions
java.lang.ClassCastException if the specified element cannot be compared with the elements currently in the set
java.lang.NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

headSet

Added in API level 9
open fun headSet(
    toElement: E,
    inclusive: Boolean
): NavigableSet<E>!
Parameters
toElement E: high endpoint of the returned set
inclusive Boolean: true if the high endpoint is to be included in the returned view
Return
NavigableSet<E>! a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement
Exceptions
java.lang.ClassCastException if toElement is not compatible with this set's comparator (or, if the set has no comparator, if toElement does not implement Comparable). Implementations may, but are not required to, throw this exception if toElement cannot be compared to elements currently in the set.
java.lang.NullPointerException if toElement is null and this set uses natural ordering, or its comparator does not permit null elements
java.lang.IllegalArgumentException if this set itself has a restricted range, and toElement lies outside the bounds of the range

headSet

Added in API level 1
open fun headSet(toElement: E): SortedSet<E>!
Parameters
toElement E: high endpoint (exclusive) of the returned set
Return
SortedSet<E>! a view of the portion of this set whose elements are strictly less than toElement
Exceptions
java.lang.ClassCastException if toElement is not compatible with this set's comparator (or, if the set has no comparator, if toElement does not implement Comparable). Implementations may, but are not required to, throw this exception if toElement cannot be compared to elements currently in the set.
java.lang.NullPointerException if toElement is null and this set uses natural ordering, or its comparator does not permit null elements
java.lang.IllegalArgumentException if this set itself has a restricted range, and toElement lies outside the bounds of the range

higher

Added in API level 9
open fun higher(e: E): E
Parameters
e E: the value to match
Return
E the least element greater than e, or null if there is no such element
Exceptions
java.lang.ClassCastException if the specified element cannot be compared with the elements currently in the set
java.lang.NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

isEmpty

Added in API level 1
open fun isEmpty(): Boolean

Returns true if this set contains no elements.

Return
Boolean true if this set contains no elements

iterator

Added in API level 1
open fun iterator(): MutableIterator<E>

Returns an iterator over the elements in this set in ascending order.

Return
MutableIterator<E> an iterator over the elements in this set in ascending order

last

Added in API level 1
open fun last(): E
Return
E the last (highest) element currently in this set
Exceptions
java.util.NoSuchElementException if this set is empty

lower

Added in API level 9
open fun lower(e: E): E
Parameters
e E: the value to match
Return
E the greatest element less than e, or null if there is no such element
Exceptions
java.lang.ClassCastException if the specified element cannot be compared with the elements currently in the set
java.lang.NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

pollFirst

Added in API level 9
open fun pollFirst(): E
Return
E the first element, or null if this set is empty

pollLast

Added in API level 9
open fun pollLast(): E
Return
E the last element, or null if this set is empty

remove

Added in API level 1
open fun remove(element: E): Boolean

Removes the specified element from this set if it is present. More formally, removes an element e such that Objects.equals(o, e), if this set contains such an element. Returns true if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)

Parameters
o object to be removed from this set, if present
Return
Boolean true if this set contained the specified element
Exceptions
java.lang.ClassCastException if the specified object cannot be compared with the elements currently in this set
java.lang.NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements
java.lang.UnsupportedOperationException if the remove operation is not supported by this set

remove

Added in API level 1
open fun remove(element: E): Boolean

Removes the specified element from this set if it is present. More formally, removes an element e such that Objects.equals(o, e), if this set contains such an element. Returns true if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)

Parameters
o object to be removed from this set, if present
Return
Boolean true if this set contained the specified element
Exceptions
java.lang.ClassCastException if the specified object cannot be compared with the elements currently in this set
java.lang.NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements
java.lang.UnsupportedOperationException if the remove operation is not supported by this set

spliterator

Added in API level 24
open fun spliterator(): Spliterator<E>

Creates a late-binding and fail-fast Spliterator over the elements in this set.

The Spliterator reports Spliterator#SIZED, Spliterator#DISTINCT, Spliterator#SORTED, and Spliterator#ORDERED. Overriding implementations should document the reporting of additional characteristic values.

The spliterator's comparator (see java.util.Spliterator#getComparator()) is null if the tree set's comparator (see comparator()) is null. Otherwise, the spliterator's comparator is the same as or imposes the same total ordering as the tree set's comparator.

Return
Spliterator<E> a Spliterator over the elements in this set

subSet

Added in API level 9
open fun subSet(
    fromElement: E,
    fromInclusive: Boolean,
    toElement: E,
    toInclusive: Boolean
): NavigableSet<E>!
Parameters
fromElement E: low endpoint of the returned set
fromInclusive Boolean: true if the low endpoint is to be included in the returned view
toElement E: high endpoint of the returned set
toInclusive Boolean: true if the high endpoint is to be included in the returned view
Return
NavigableSet<E>! a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive
Exceptions
java.lang.ClassCastException if fromElement and toElement cannot be compared to one another using this set's comparator (or, if the set has no comparator, using natural ordering). Implementations may, but are not required to, throw this exception if fromElement or toElement cannot be compared to elements currently in the set.
java.lang.NullPointerException if fromElement or toElement is null and this set uses natural ordering, or its comparator does not permit null elements
java.lang.IllegalArgumentException if fromElement is greater than toElement; or if this set itself has a restricted range, and fromElement or toElement lies outside the bounds of the range.

subSet

Added in API level 1
open fun subSet(
    fromElement: E,
    toElement: E
): SortedSet<E>!
Parameters
fromElement E: low endpoint (inclusive) of the returned set
toElement E: high endpoint (exclusive) of the returned set
Return
SortedSet<E>! a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive
Exceptions
java.lang.ClassCastException if fromElement and toElement cannot be compared to one another using this set's comparator (or, if the set has no comparator, using natural ordering). Implementations may, but are not required to, throw this exception if fromElement or toElement cannot be compared to elements currently in the set.
java.lang.NullPointerException if fromElement or toElement is null and this set uses natural ordering, or its comparator does not permit null elements
java.lang.IllegalArgumentException if fromElement is greater than toElement; or if this set itself has a restricted range, and fromElement or toElement lies outside the bounds of the range

tailSet

Added in API level 9
open fun tailSet(
    fromElement: E,
    inclusive: Boolean
): NavigableSet<E>!
Parameters
fromElement E: low endpoint of the returned set
inclusive Boolean: true if the low endpoint is to be included in the returned view
Return
NavigableSet<E>! a view of the portion of this set whose elements are greater than or equal to fromElement
Exceptions
java.lang.ClassCastException if fromElement is not compatible with this set's comparator (or, if the set has no comparator, if fromElement does not implement Comparable). Implementations may, but are not required to, throw this exception if fromElement cannot be compared to elements currently in the set.
java.lang.NullPointerException if fromElement is null and this set uses natural ordering, or its comparator does not permit null elements
java.lang.IllegalArgumentException if this set itself has a restricted range, and fromElement lies outside the bounds of the range

tailSet

Added in API level 1
open fun tailSet(fromElement: E): SortedSet<E>!
Parameters
fromElement E: low endpoint (inclusive) of the returned set
Return
SortedSet<E>! a view of the portion of this set whose elements are greater than or equal to fromElement
Exceptions
java.lang.ClassCastException if fromElement is not compatible with this set's comparator (or, if the set has no comparator, if fromElement does not implement Comparable). Implementations may, but are not required to, throw this exception if fromElement cannot be compared to elements currently in the set.
java.lang.NullPointerException if fromElement is null and this set uses natural ordering, or its comparator does not permit null elements
java.lang.IllegalArgumentException if this set itself has a restricted range, and fromElement lies outside the bounds of the range

Properties

size

Added in API level 1
open val size: Int

Returns the number of elements in this set (its cardinality).

Return
Int the number of elements in this set (its cardinality)