📜  Java.util.TreeSet类

📅  最后修改于: 2020-11-14 06:38:18             🧑  作者: Mango


介绍

java.util.TreeSet类实现Set接口。以下是有关TreeSet的要点-

  • TreeSet类保证Map将按升序排列并由TreeMap支持。

  • 根据键类的自然排序方法或在集合创建时提供的Comparator来对Map进行排序,这取决于所使用的构造函数。

  • 排序必须总在为了使树函数工作。

类声明

以下是java.util.TreeSet类的声明-

public class TreeSet
   extends AbstractSet
   implements NavigableSet, Cloneable, Serializable

参量

以下是java.util.TreeSet类的参数-

E-这是此集合维护的元素的类型。

类的构造函数

Sr.No. Constructor & Description
1

TreeSet()

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

2

TreeSet(Collection c)

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

3

TreeSet(Comparator comparator)

This constructor constructs a new, empty tree set, sorted according to the specified comparator.

4

TreeSet(SortedSet s)

This constructor constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.

类方法

Sr.No. Method & Description
1 boolean add(E e)

This method adds the specified element to this set if it is not already present.

2 boolean addAll(Collection c)

This method adds all of the elements in the specified collection to this set.

3 E ceiling(E e)

This method returns the least element in this set greater than or equal to the given element, or null if there is no such element.

4 void clear()

This method removes all of the elements from this set.

5 Object clone()

This method returns a shallow copy of this TreeSet instance.

6 Comparator comparator()

This method returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.

7 boolean contains(Object o)

This method returns true if this set contains the specified element.

8 Iterator descendingIterator()

This method returns an iterator over the elements in this set in descending order.

9 NavigableSet descendingSet()

This method returns a reverse order view of the elements contained in this set.

10 E first()

This method returns the first (lowest) element currently in this set.

11 E floor(E e)

This method Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.

12 SortedSet headSet(E toElement)

This method returns a view of the portion of this set whose elements are strictly less than toElement.

13 NavigableSet headSet(E toElement, boolean inclusive)

This method Returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement.

14 E higher(E e)

This method returns the least element in this set strictly greater than the given element, or null if there is no such element.

15 boolean isEmpty()

This method returns true if this set contains no elements.

16 Iterator iterator()

This method returns an iterator over the elements in this set in ascending order.

17 E last()

This method returns the last (highest) element currently in this set.

18 E lower(E e)

This method returns the greatest element in this set strictly less than the given element, or null if there is no such element.

19 E pollFirst()

This method retrieves and removes the first (lowest) element, or returns null if this set is empty.

20 E pollLast()

This method retrieves and removes the last (highest) element, or returns null if this set is empty.

21 boolean remove(Object o)

This method removes the specified element from this set if it is present.

22 int size()

This method returns the number of elements in this set (its cardinality).

23 NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

This method returns a view of the portion of this set whose elements range from fromElement to toElement.

24 SortedSet subSet(E fromElement, E toElement)

This method returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

25 SortedSet tailSet(E fromElement)

This method returns a view of the portion of this set whose elements are greater than or equal to fromElement.

26 NavigableSet tailSet(E fromElement, boolean inclusive)

This method returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement.

方法继承

此类从以下类继承方法-

  • java.util.AbstractSet
  • java.util.AbstractCollection
  • java.util.Object
  • java.util.Set