What is the difference between them? I know that
A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. Use this class instead of HashSet when you care about the iteration order. When you iterate through a HashSet the order is unpredictable, while a LinkedHashSet lets you iterate through the elements in the order in which they were inserted.
But in sourcecode of LinkedHashSet there are only calling constructors of HashSet.开发者_如何学JAVA So where is double-linked List and insertion order?
The difference between the two are, as you've stated:
A
LinkedHashSet
is an ordered version ofHashSet
that maintains a doubly-linked List across all elements. Use this class instead ofHashSet
when you care about the iteration order. When you iterate through aHashSet
the order is unpredictable, while aLinkedHashSet
lets you iterate through the elements in the order in which they were inserted.
As for your question:
But in sourcecode of LinkedHashSet there are only calling constructors of HashSet.
The answer lies in which constructors the LinkedHashSet
uses to construct the base class:
public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true); // <-- boolean dummy argument
}
...
public LinkedHashSet(int initialCapacity) {
super(initialCapacity, .75f, true); // <-- boolean dummy argument
}
...
public LinkedHashSet() {
super(16, .75f, true); // <-- boolean dummy argument
}
...
public LinkedHashSet(Collection<? extends E> c) {
super(Math.max(2*c.size(), 11), .75f, true); // <-- boolean dummy argument
addAll(c);
}
And (one example of) a HashSet
constructor that takes a boolean argument is described, and looks like this:
/**
* Constructs a new, empty linked hash set. (This package private
* constructor is only used by LinkedHashSet.) The backing
* HashMap instance is a LinkedHashMap with the specified initial
* capacity and the specified load factor.
*
* @param initialCapacity the initial capacity of the hash map
* @param loadFactor the load factor of the hash map
* @param dummy ignored (distinguishes this
* constructor from other int, float constructor.)
* @throws IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive
*/
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor);
}
HashSet is unordered and unsorted Set.
LinkedHashSet is the ordered version of HashSet.
The only difference between HashSet and LinkedHashSet is that:
LinkedHashSet maintains the insertion order.
When we iterate through a HashSet, the order is unpredictable while it is predictable in case of LinkedHashSet.
The reason for how LinkedHashSet maintains insertion order is that:
The underlying used data structure is Doubly-Linked-List.
LinkedHashSet
's constructors invoke the following base class constructor:
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<E, Object>(initialCapacity, loadFactor);
}
As you can see, the internal map is a LinkedHashMap
. If you look inside LinkedHashMap
, you'll discover the following field:
private transient Entry<K, V> header;
This is the linked list in question.
I suggest you to use LinkedHashSet
most of the time, because it has better performance overall):
- Predictable iteration order LinkedHashSet (Oracle)
- LinkedHashSet is more expensive for insertions than HashSet;
- In general slightly better performance than
HashMap
, because the most of the time we use Set structures for iterating.
Performance tests:
------------- TreeSet -------------
size add contains iterate
10 746 173 89
100 501 264 68
1000 714 410 69
10000 1975 552 69
------------- HashSet -------------
size add contains iterate
10 308 91 94
100 178 75 73
1000 216 110 72
10000 711 215 100
---------- LinkedHashSet ----------
size add contains iterate
10 350 65 83
100 270 74 55
1000 303 111 54
10000 1615 256 58
You can see source test page here: The Final Performance Testing Example
You should look at the source of the HashSet
constructor it calls... it's a special constructor that makes the backing Map
a LinkedHashMap
instead of just a HashMap
.
HashSet: Unordered actually. if u passing the parameter means
Set<Integer> set=new HashSet<Integer>();
for(int i=0;i<set.length;i++)
{
SOP(set)`enter code here`
}
Out Put:
May be 2,1,3
not predictable. next time another order.
LinkedHashSet()
which produce FIFO Order.
HashSet:
The underlined data structure is Hashtable. Duplicate objects are not allowed.insertion order is not preserved and it is based on hash code of objects. Null insertion is possible(only once). It implements Serializable, Clonable but not RandomAccess interface. HashSet is best choose if frequent operation is search operation.
In HashSet duplicates are not allowed.if users are trying to insert duplicates when we won't get any compile or runtime exceptions. add method returns simply false.
Constructors:
HashSet h=new HashSet(); creates an empty HashSet object with default initial capacity 16 and default fill ratio(Load factor) is 0.75 .
HashSet h=new HashSet(int initialCapacity); creates an empty HashSet object with specified initialCapacity and default fill ration is 0.75.
HashSet h=new HashSet(int initialCapacity, float fillRatio);
HashSet h=new HashSet(Collection c); creates an equivalent HashSet object for the given collection. This constructor meant for inter conversion between collection object.
LinkedHashSet:
It is a child class of HashSet. it is exactly same as HashSet including(Constructors and Methods) except the following differences.
Differences HashSet:
- The underlined data structure is Hashtable.
- Insertion order is not preserved.
- introduced 1.2 version.
LinkedHashSet:
- The underlined data structure is a combination of LinkedList and Hashtable.
- Insertion order is preserved.
- Indroduced in 1.4 version.
HashSet
don't maintain the order of insertion item
LinkedHashSet
maintain the order of insertion item
Example
Set<String> set = ...;// using new HashSet<>() OR new LinkedHashSet<>()
set.add("2");
set.add("1");
set.add("ab");
for(String value : set){
System.out.println(value);
}
HashSet
output
1
ab
2
LinkedHashSet
output
2
1
ab
If you take a look at the constructors called from the LinkedHashSet
class you will see that internally it's a LinkedHashMap
that is used for backing purpose.
All Methods and constructors are same but only one difference is LinkedHashset will maintain insertion order but it will not allow duplicates.
Hashset will not maintain any insertion order. It is combination of List and Set simple :)
精彩评论