Java中的HashSet与TreeSet (HashSet vs TreeSet in Java)
First, we will see how TreeSet differs from HashSet in Java?
首先,我们将看到TreeSet与Java中的HashSet有何不同?
树集 (TreeSet)
TreeSet is available in java.util package.
TreeSet在java.util包中可用。
TreeSet is an implementation class of Set interface.
TreeSet是Set接口的实现类。
The underlying data structure of TreeSet is Balanced Tree.
TreeSet的基础数据结构是Balanced Tree。
In TreeSet "insertion order of the elements" is not preserved because elements will be inserted in TreeSet according to some ascending sorting order or we can say in other words "insertion order of the elements" is not needed to be the same as the "retrieval order of the elements".
在TreeSet中,不保留“元素的插入顺序”,因为元素将按照某种升序排序插入TreeSet中,或者换句话说,“元素的插入顺序”不需要与“检索”相同元素的顺序”。
In TreeSet Object is represented as a group of individual elements as a single entity in term of values only.
在TreeSet中,对象仅作为值表示为一组作为单个实体的单个元素。
In TreeSet "duplicate elements are not allowed" that means it is not possible to insert duplicate elements in TreeSet.
在TreeSet中,“不允许使用重复的元素”,这意味着无法在TreeSet中插入重复的元素。
In TreeSet "null insertion is not possible " for a non-empty set.
在TreeSet中,对于非空集,“不可能插入空”。
In TreeSet "null insertion is possible" for empty Set as the first element and if we insert null after inserting the first element then it is not possible or invalid.
在TreeSet中,将空Set设置为第一个元素“可以插入null”,如果在插入第一个元素之后插入null,则不可能或无效。
In TreeSet "Heterogenous objects" are not allowed and if will insert forcefully then we will get an exception "ClassCastException".
在TreeSet中,不允许使用“异构对象”,如果强行插入,则会得到异常“ ClassCastException”。
Example:
例:
// Java program to demonstrate the behavior of TreeSet
import java.util.*;
class TreeSetClass {
public static void main(String[] args) {
// Creating an instance of TreeSet
TreeSet ts = new TreeSet();
// By using add() to add elements in TreeSet
ts.add(10);
ts.add(30);
ts.add(40);
ts.add(20);
/* ts.add(30);
Here will not get any exception or errors
but it will be ignored because duplicate
insertion is not possible */
/* ts.add(null);
here we will get an exception NullPointerException
because we are inserting null for non-empty set */
/* ts.add("Java");
here we will get an exception ClassCastException
because we are inserting hetrogenous object in TreeSet */
// Display Current TreeSet
System.out.println("Current TreeSet is :" + ts);
}
}
Output
输出量
E:\Programs>javac TreeSetClass.java
E:\Programs>java TreeSetClass
Current TreeSet is :[10, 20, 30, 40]
Second, we will see how HashSet differs from TreeSet in Java?
其次,我们将看到HashSet与Java中的TreeSet有何不同?
哈希集 (HashSet)
HashSet is available in java.util package.
HashSet在java.util包中可用。
HashSet is an implementation class of Set interface.
HashSet是Set接口的实现类。
HashSet is a parent class of LinkedHashSet.
HashSet是LinkedHashSet的父类。
The underlying data structure of TreeSet is Hashtable.
TreeSet的基础数据结构是Hashtable。
In HashSet "insertion order of the elements" is not preserved or we can say in other words "insertion order of the elements" is not needed to be the same as the "retrieval order of the elements".
在HashSet中,“元素的插入顺序”未保留,或者换句话说,“元素的插入顺序”不需要与“元素的检索顺序”相同。
In HashSet Object is represented as a group of individual elements as a single entity in term of values only.
在HashSet中,对象仅作为值表示为一组单个元素,作为单个实体。
In HashSet "duplicate elements are not allowed" that means it is not possible to insert duplicate elements in HashSet.
在HashSet中,“不允许使用重复的元素”,这意味着无法在HashSet中插入重复的元素。
In HashSet "null insertion is possible " for the non-empty and empty set.
在HashSet中,对于非空集和空集,“可能插入空”。
In HashSet "Heterogenous objects" are allowed and if will insert forcefully then we will not get any exception.
在HashSet中,允许“异构对象”,如果将其强制插入,则不会出现任何异常。
Example:
例:
// Java program to demonstrate the behavior of HashSet
import java.util.*;
class HashSetClass {
public static void main(String[] args) {
// Creating an instance of HashSet
HashSet hs = new HashSet();
// By using add() method to add elements in HashSet
hs.add(10);
hs.add(30);
hs.add(40);
hs.add(20);
/* Here we will not get any exception because
null insertion is possible in HashSet
*/
hs.add(null);
/* Here will not get any exception or errors
but it will be ignored because duplicate insertion
is not possible
*/
hs.add(30);
/* Here we will not get any exception because hetrogenous
object insertion is possible in HashSet
*/
hs.add("Java");
// Display Current HashSet
System.out.println("Current HashSet is :" + hs);
}
}
Output
输出量
E:\Programs>javac HashSetClass.java
E:\Programs>java HashSetClass
Current HashSet is :[null, 20, 40, 10, Java, 30]
翻译自: https://www.includehelp.com/java/differences-between-hashset-and-treeset-in-java.aspx