📅  最后修改于: 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 extends E> 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 super E> comparator) This constructor constructs a new, empty tree set, sorted according to the specified comparator. |
4 |
TreeSet(SortedSet 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 extends E> 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 super E> 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 This method returns an iterator over the elements in this set in descending order. |
9 | NavigableSet 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 This method returns a view of the portion of this set whose elements are strictly less than toElement. |
13 | NavigableSet 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 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 This method returns a view of the portion of this set whose elements range from fromElement to toElement. |
24 | SortedSet This method returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. |
25 | SortedSet This method returns a view of the portion of this set whose elements are greater than or equal to fromElement. |
26 | NavigableSet This method returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement. |
此类从以下类继承方法-