📅  最后修改于: 2020-10-12 09:58:39             🧑  作者: Mango
Java TreeSet类实现了Set接口,该接口使用树进行存储。它继承了AbstractSet类并实现了NavigableSet接口。 TreeSet类的对象以升序存储。
关于Java TreeSet类的要点是:
如上图所示,Java TreeSet类实现了NavigableSet接口。 NavigableSet接口按层次结构顺序扩展了SortedSet,Set,Collection和Iterable接口。
我们来看一下java.util.TreeSet类的声明。
public class TreeSet extends AbstractSet implements NavigableSet, Cloneable, Serializable
Constructor | Description |
---|---|
TreeSet() | It is used to construct an empty tree set that will be sorted in ascending order according to the natural order of the tree set. |
TreeSet(Collection extends E> c) | It is used to build a new tree set that contains the elements of the collection c. |
TreeSet(Comparator super E> comparator) | It is used to construct an empty tree set that will be sorted according to given comparator. |
TreeSet(SortedSet |
It is used to build a TreeSet that contains the elements of the given SortedSet. |
Method | Description |
---|---|
boolean add(E e) | It is used to add the specified element to this set if it is not already present. |
boolean addAll(Collection extends E> c) | It is used to add all of the elements in the specified collection to this set. |
E ceiling(E e) | It returns the equal or closest greatest element of the specified element from the set, or null there is no such element. |
Comparator super E> comparator() | It returns comparator that arranged elements in order. |
Iterator |
It is used iterate the elements in descending order. |
NavigableSet |
It returns the elements in reverse order. |
E floor(E e) | It returns the equal or closest least element of the specified element from the set, or null there is no such element. |
SortedSet |
It returns the group of elements that are less than the specified element. |
NavigableSet |
It returns the group of elements that are less than or equal to(if, inclusive is true) the specified element. |
E higher(E e) | It returns the closest greatest element of the specified element from the set, or null there is no such element. |
Iterator |
It is used to iterate the elements in ascending order. |
E lower(E e) | It returns the closest least element of the specified element from the set, or null there is no such element. |
E pollFirst() | It is used to retrieve and remove the lowest(first) element. |
E pollLast() | It is used to retrieve and remove the highest(last) element. |
Spliterator |
It is used to create a late-binding and fail-fast spliterator over the elements. |
NavigableSet |
It returns a set of elements that lie between the given range. |
SortedSet |
It returns a set of elements that lie between the given range which includes fromElement and excludes toElement. |
SortedSet |
It returns a set of elements that are greater than or equal to the specified element. |
NavigableSet |
It returns a set of elements that are greater than or equal to (if, inclusive is true) the specified element. |
boolean contains(Object o) | It returns true if this set contains the specified element. |
boolean isEmpty() | It returns true if this set contains no elements. |
boolean remove(Object o) | It is used to remove the specified element from this set if it is present. |
void clear() | It is used to remove all of the elements from this set. |
Object clone() | It returns a shallow copy of this TreeSet instance. |
E first() | It returns the first (lowest) element currently in this sorted set. |
E last() | It returns the last (highest) element currently in this sorted set. |
int size() | It returns the number of elements in this set. |
让我们看一个简单的Java TreeSet示例。
import java.util.*;
class TreeSet1{
public static void main(String args[]){
//Creating and adding elements
TreeSet al=new TreeSet();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
//Traversing elements
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
输出:
Ajay
Ravi
Vijay
让我们看一个以降序遍历元素的示例。
import java.util.*;
class TreeSet2{
public static void main(String args[]){
TreeSet set=new TreeSet();
set.add("Ravi");
set.add("Vijay");
set.add("Ajay");
System.out.println("Traversing element through Iterator in descending order");
Iterator i=set.descendingIterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
输出:
Traversing element through Iterator in descending order
Vijay
Ravi
Ajay
Traversing element through NavigableSet in descending order
Vijay
Ravi
Ajay
让我们看一个示例来检索和删除最高和最低价值。
import java.util.*;
class TreeSet3{
public static void main(String args[]){
TreeSet set=new TreeSet();
set.add(24);
set.add(66);
set.add(12);
set.add(15);
System.out.println("Highest Value: "+set.pollFirst());
System.out.println("Lowest Value: "+set.pollLast());
}
}
输出:
Highest Value: 12
Lowest Value: 66
在此示例中,我们执行各种NavigableSet操作。
import java.util.*;
class TreeSet4{
public static void main(String args[]){
TreeSet set=new TreeSet();
set.add("A");
set.add("B");
set.add("C");
set.add("D");
set.add("E");
System.out.println("Initial Set: "+set);
System.out.println("Reverse Set: "+set.descendingSet());
System.out.println("Head Set: "+set.headSet("C", true));
System.out.println("SubSet: "+set.subSet("A", false, "E", true));
System.out.println("TailSet: "+set.tailSet("C", false));
}
}
输出:
Initial Set: [A, B, C, D, E]
Reverse Set: [E, D, C, B, A]
Head Set: [A, B, C]
SubSet: [B, C, D, E]
TailSet: [D, E]
在此示例中,我们执行各种SortedSetSet操作。
import java.util.*;
class TreeSet4{
public static void main(String args[]){
TreeSet set=new TreeSet();
set.add("A");
set.add("B");
set.add("C");
set.add("D");
set.add("E");
System.out.println("Intial Set: "+set);
System.out.println("Head Set: "+set.headSet("C"));
System.out.println("SubSet: "+set.subSet("A", "E"));
System.out.println("TailSet: "+set.tailSet("C"));
}
}
输出:
Intial Set: [A, B, C, D, E]
Head Set: [A, B]
SubSet: [A, B, C, D]
TailSet: [C, D, E]
让我们看一个TreeSet示例,其中我们要添加书籍以进行设置并打印所有书籍。 TreeSet中的元素必须为Comparable类型。默认情况下,String和Wrapper类是Comparable的。要在TreeSet中添加用户定义的对象,您需要实现Comparable接口。
import java.util.*;
class Book implements Comparable{
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
public int compareTo(Book b) {
if(id>b.id){
return 1;
}else if(id set=new TreeSet();
//Creating Books
Book b1=new Book(121,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(233,"Operating System","Galvin","Wiley",6);
Book b3=new Book(101,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
//Adding Books to TreeSet
set.add(b1);
set.add(b2);
set.add(b3);
//Traversing TreeSet
for(Book b:set){
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
}
}
}
输出:
101 Data Communications & Networking Forouzan Mc Graw Hill 4
121 Let us C Yashwant Kanetkar BPB 8
233 Operating System Galvin Wiley 6