Java中的TreeSet被认为是有助于存储的SortedSet接口的主要实现之一。在此,元素以自然顺序或基于显式比较器的顺序设置的方式排序。 TreeSet继承了AbstractSet类并实现了NavigableSet接口。尽管它与 HashSet 类似,但它维护顺序,而 HashSet 不维护顺序。此外,与 HashSet 不同,TreeSet 允许使用 null 元素。有关 TreeSet 的更多详细信息将在以下部分中讨论。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
TreeSet 的特点
以下是TreeSet的主要功能。
- 不允许重复值。
- 对象按升序排序。
- 不保留插入顺序。除此之外,元素根据键进行排序。
- 不允许插入异构对象。如果我们尝试插入,则会在运行时抛出 classCastException。
- 存储大量排序数据。
- 快速检索时间。
- 快速访问。
- 不允许使用空元素。
- 添加、搜索和删除等操作需要 O(log n) 的时间。
- 在打印n个元素等操作时,时间复杂度会是O(n)。
- 不同步。
- TreeMap 的存在是为了存储元素。
- 不是线程安全的。
语法:
为了创建TreeSet,首先导入包java.util.TreeSet。之后,使用语法创建一个 TreeSet:
TreeSet<Integer> num= new TreeSet<>();
登录后复制
由于我们在创建 TreeSet 时没有提及任何参数,因此元素将按照其自然顺序进行排序。即升序。
正如已经提到的,可以使用 Comparator 接口来自定义排序。
Java 中 TreeSet 的构造函数
Java中的TreeSet有4个构造函数。他们是:
-
TreeSet(): 将创建一个新的空 TreeSet,并根据自然顺序进行排序。
-
TreeSet(Collection extends E> c): 将使用集合 c 中提到的元素创建一个新的 TreeSet,并且它们将根据自然顺序进行排序。
-
TreeSet(Comparator super E> 比较器): 将根据提到的比较器排序创建一个新的空 TreeSet。
-
TreeSet(SortedSet s):将使用 SortedSet 中提到的元素创建一个新的 TreeSet,其顺序与其相同。
Java 中 TreeSet 的方法
TreeSet 中有几个需要执行的函数。让我们看看它们是什么。
-
add(Ee): An element e will be added to the set if it is not present in it.
-
addAll(Collection extends E> c): All the elements in collection c will be added to the set.
-
E ceiling(Ee): The last element which is greater than or equal to the element in the set will be returned.
-
clear(): All the elements in the set will be removed.
-
clone(): A shallow copy will be returned for the TreeSet instance.
-
comparator(): The comparator used in the set will be returned. If natural ordering is used, null will be returned.
-
contains(Objecto): If the set contains the element o, true will be returned.
-
descendingIterator(): An iterator will be returned over the elements in descending order.
-
descendingSet(): A reverse order view will be returned for the elements present in the list.
-
first(): he first or the lowest element in the set will be returned.
-
last(): The last or the largest element in the set will be returned.
-
iterator(): n iterator will be returned over the elements in ascending order.
-
lower(Ee): The greatest element will be returned which is strictly small than the element e which is given. If there is no such element, null will be returned.
-
higher(Ee): The smallest element will be returned which is strictly high than the element e which is given. If there is no such element, null will be returned.
-
isEmpty(): True will be returned if no elements are present.
-
size(): The number of elements in the set will be returned. In other words, cardinality will be returned.
-
pollFirst(): The first or the lowest element in the set will be retrieved and removed. If there are no elements in the set, the null will be returned.
-
pollLast(): The last or the highest element in the set will be retrieved and removed. If there are no elements in the set, the null will be returned.
-
remove(Objecto): If the set contains the element o, it will be removed.
-
subSet(EfromElement, boolean fromInclusive, E toElement, boolean toInclusive): A view will be returned for the portion of the set from the range from Element to toElement.
-
subSet(EfromElement, E toElement): A view will be returned for the portion of the set from the range fromElement(inclusive) to toElement(exclusive).
-
tailSet(EfromElement): view will be returned for the portion of the set where elements are larger than fromElement.
-
tailSet(EfromElement, boolean inclusive): A view will be returned for the portion of the set where elements are larger than fromElement. It is considered if inclusive is true.
Example of TreeSet in Java
Java program to create a tree set with the natural ordering of elements.
import java.util.SortedSet;
import java.util.TreeSet;
//class
public class TreeSetExample {
//main method
public static void main(String[] args) {
// TreeSet <u>fam</u> creation
SortedSet<String> fam = new TreeSet<>();
// Adding new elements to a TreeSet
fam.add("Anna");
fam.add("Adam");
fam.add("Sam");
fam.add("Iza");
//print the treeset
System.out.println("Fam Set : " + fam);
// Trying to add duplicate element <u>Anna</u>
fam.add("Anna");
System.out.println("Added element Anna, Now the treeset is : " + fam);
// Trying to add element <u>Anna</u> with lower case
fam.add("anna");
System.out.println("Added element anna, Now the treeset is : " + fam);
}
}
登录后复制
Output:
A treeset fam is created first. Once it is created, elements are added to it and the whole treeset is printed. After this, an element ‘Anna’ which is already present in the treeset is trying to get added. Since duplicates are not allowed in TreeSet, it is not get added. After that, an element ‘anna’ which is the lower case of the already existing element ‘Anna’ is added. Since it is in lowercase, it gets added to the treeset without any problem.
Conclusion
TreeSet in Java is an implementation of the interface SortedSet that helps in storage where the elements are arranged in natural ordering or based on the comparator. Several aspects of TreeSet is discussed in this document.
以上是Java 中的树集的详细内容。更多信息请关注PHP中文网其他相关文章!