Java中HashSet、LinkedHashSet、TreeSet的异同
在本文中,我们将了解 HashSet 与 LinkedHashSet 和 TreeSet 之间的区别以及 LinkedHashSet 和 TreeSet 之间的相似之处。 HashSet、LinkedHashSet 和 TreeSet 都实现了 Set 接口。所以我们试图列出Java中HashSet、LinkedHashSet和TreeSet之间的区别和相似之处。
HashSet、LinkedHashSet和 TreeSet 的区别: Features HashSet LinkedHashSet TreeSet Internal Working HashSet internally uses HashMap for storing objects LinkedHashSet uses LinkedHashMap internally to store objects TreeSet uses TreeMap internally to store objects When To Use If you don’t want to maintain insertion order but want to store unique objects If you want to maintain the insertion order of elements then you can use LinkedHashSet If you want to sort the elements according to some Comparator then use TreeSet Order HashSet does not maintain insertion order LinkedHashSet maintains the insertion order of objects While TreeSet orders the elements according to supplied Comparator. By default, objects will be placed according to their natural ascending order. Complexity of Operations HashSet gives O(1) complexity for insertion, removing, and retrieving objects LinkedHashSet gives insertion, removing, and retrieving operations performance in order O(1). While TreeSet gives the performance of order O(log(n)) for insertion, removing, and retrieving operations. Performance The performance of HashSet is better when compared to LinkedHashSet and TreeSet. The performance of LinkedHashSet is slower than TreeSet. It is almost similar to HashSet but slower because LinkedHashSet internally maintains LinkedList to maintain the insertion order of elements TreeSet performance is better than LinkedHashSet except for insertion and removal operations because it has to sort the elements after each insertion and removal operation. Compare HashSet uses equals() and hashCode() methods to compare the objects LinkedHashSet uses equals() and hashCode() methods to compare it’s objects TreeSet uses compare() and compareTo() methods to compare the objects Null Elements HashSet allows only one null value. LinkedHashSet allows only one null value. TreeSet does not permit null value. If you insert null value into TreeSet, it will throw NullPointerException. Syntax HashSet obj = new HashSet(); LinkedHashSet obj = new LinkedHashSet(); TreeSet obj = new TreeSet();
HashSet、LinkedHashSet和 TreeSet 根据插入顺序和所用时间的区别:
Java
// Java program to demonstrate difference between
// HashSet, LinkedHashSet and TreeSet according
// to insertion order and insertion time
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
class GFG1 {
// Function show insertion order of
// LinkedHashSet, TreeSet and HashSet
private static void insertionOrder()
{
LinkedHashSet geekLinkSet
= new LinkedHashSet<>();
TreeSet geekTreeSet = new TreeSet<>();
HashSet geekHashSet = new HashSet();
// Add three object in
// LinkedHashSet and TreeSet
for (String str : Arrays.asList("Geek2", "Geek1",
"Geek3", "Geek1")) {
geekLinkSet.add(str);
geekTreeSet.add(str);
geekHashSet.add(str);
}
// should be sorted order HashSet
// stores element in sorted order
System.out.println("Insertion Order"
+ " of objects in HashSet :"
+ geekHashSet);
// insertion order or elements LinkedHashSet
// storeds elements as insertion
System.out.println("Insertion Order of "
+ "objects in LinkedHashSet :"
+ geekLinkSet);
// should be sorted order TreeSet
// stores element in sorted order
System.out.println("Insertion Order of"
+ " objects in TreeSet :"
+ geekTreeSet);
}
// Function calculate insertion time of
// 1000 objects of LinkedHashSet,
// TreeSet and HashSet
private static void insertionTime()
{
// HashSet performance Test
// inserting 1000 elements
HashSet numbersHS = new HashSet<>();
long startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
numbersHS.add(i);
}
long endTime = System.nanoTime();
System.out.println("Total time to insert"
+ " 1000 elements in"
+ " HashSet in nanoseconds: "
+ (endTime - startTime));
// LinkedHashSet performance Test
// inserting 1000 elements
LinkedHashSet numbersLLS
= new LinkedHashSet<>();
startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
numbersLLS.add(i);
}
endTime = System.nanoTime();
System.out.println("Total time to insert"
+ " 1000 elements in"
+ " LinkedHashSet nanoseconds: "
+ (endTime - startTime));
// TreeSet performance Test inserting 1000 objects
TreeSet numbersTS = new TreeSet<>();
startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
numbersTS.add(i);
}
endTime = System.nanoTime();
System.out.println("Total time to insert"
+ " 1000 elements in"
+ " TreeSet in nanoseconds: "
+ (endTime - startTime));
}
// Function calculate deletion time
// of 1000 objects LinkedHashSet,
// TreeSet and HashSet
// Deletion time always vary
private static void deletion()
{
// HashSet performance Test inserting
// and deletion 1000 elements
HashSet deletionHS = new HashSet<>();
for (int i = 0; i < 1000; i++) {
deletionHS.add(i);
}
long startingTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
deletionHS.remove(i);
}
long endedTime = System.nanoTime();
System.out.println(
"Total time to Deletion "
+ "1000 elements in HashSet in nanoseconds: "
+ Math.abs(startingTime - endedTime));
// LinkedHashSet performance Test inserting
// and deletion 1000 elements
LinkedHashSet deletionLLS
= new LinkedHashSet<>();
for (int i = 0; i < 1000; i++) {
deletionLLS.add(i);
}
startingTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
deletionLLS.remove(i);
}
endedTime = System.nanoTime();
System.out.println(
"Total time to Deletion 1000"
+ " elements in LinkedHashSet in nanoseconds: "
+ Math.abs(startingTime - endedTime));
// TreeSet performance Test inserting
// and deletion 1000 elements
TreeSet deletionTS = new TreeSet<>();
for (int i = 0; i < 1000; i++) {
deletionTS.add(i);
}
startingTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
deletionTS.remove(i);
}
endedTime = System.nanoTime();
System.out.println(
"Total time to Deletion 1000"
+ " elements in TreeSet in nanoseconds: "
+ Math.abs(startingTime - endedTime));
}
public static void main(String args[])
{
insertionOrder();
insertionTime();
deletion();
}
}
Insertion Order of objects in HashSet :[Geek3, Geek2, Geek1]
Insertion Order of objects in LinkedHashSet :[Geek2, Geek1, Geek3]
Insertion Order of objects in TreeSet :[Geek1, Geek2, Geek3]
Total time to insert 1000 elements in HashSet in nanoseconds: 791869
Total time to insert 1000 elements in LinkedHashSet nanoseconds: 882417
Total time to insert 1000 elements in TreeSet in nanoseconds: 11797657
Total time to Deletion 1000 elements in HashSet in nanoseconds: 834509
Total time to Deletion 1000 elements in LinkedHashSet in nanoseconds: 898922
Total time to Deletion 1000 elements in TreeSet in nanoseconds: 7437577
HashSet、LinkedHashSet和 TreeSet 之间的相似之处:
- Duplicates:HashSet、LinkedHashSet 和 TreeSet 都实现了 Set 接口,因此不允许存储重复对象。
- 线程安全:如果我们想在多线程环境中使用 HashSet、LinkedHashSet 和 TreeSet,那么首先我们使其外部同步,因为 LinkedHashSet 和 TreeSet 都不是线程安全的。
- 这三个都是可克隆和可序列化的。
何时在Java中使用 HashSet、TreeSet 和 LinkedHashSet:
- HashSet:如果您不想维护插入顺序但想存储唯一对象。
- LinkedHashSet:如果要保持元素的插入顺序,可以使用 LinkedHashSet。
- TreeSet:如果要根据某个 Comparator 对元素进行排序,请使用 TreeSet。
因此,当您看到上述程序的输出时,根据您的要求,您可以从 HashSet、TreeSet 和 LinkedHashSet 中选择任何一个。