下面是一个简单的TreeSet的例子,通过这个简单的例子,你将了解:
- TreeSet是有序的
- 如何遍历一个TreeSet
- 如何检查空
- 如何删除一个元素
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetExample {
public static void main(String[] args) {
System.out.println("Tree Set Example!\n");
TreeSet<Integer> tree = new TreeSet<Integer>();
tree.add(12);
tree.add(63);
tree.add(34);
tree.add(45);
// here it test it's sorted, 63 is the last element. see output below
Iterator<Integer> iterator = tree.iterator();
System.out.print("Tree set data: ");
// Displaying the Tree set data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
// Check empty or not
if (tree.isEmpty()) {
System.out.print("Tree Set is empty.");
} else {
System.out.println("Tree Set size: " + tree.size());
}
// Retrieve first data from tree set
System.out.println("First data: " + tree.first());
// Retrieve last data from tree set
System.out.println("Last data: " + tree.last());
if (tree.remove(45)) { // remove element by value
System.out.println("Data is removed from tree set");
} else {
System.out.println("Data doesn't exist!");
}
System.out.print("Now the tree set contain: ");
iterator = tree.iterator();
// Displaying the Tree set data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Now the size of tree set: " + tree.size());
// Remove all
tree.clear();
if (tree.isEmpty()) {
System.out.print("Tree Set is empty.");
} else {
System.out.println("Tree Set size: " + tree.size());
}
}
}
输出:
Tree Set Example!
Tree set data: 12 34 45 63
Tree Set size: 4
First data: 12
Last data: 63
Data is removed from tree set
Now the tree set contain: 12 34 63
Now the size of tree set: 3
Tree Set is empty.