I just understand that LinkedHashSet does not allows duplicate elements when it is inserting. But, I dont understand how does Hashset works in Hava? I know a bit that Hashtable is used in Hashset so the hashtable used to store the elements and here also does not allow the duplicate elements. Then, Treeset is also similar to Hashset it also does not allows duplicate entries so unique elements will be seen and it follows ascending order.
I have one more doubt regarding HashMap - Hashmap does not maintains order. It may have one null key and multiple null values. I just dont understand this and what does it mean actually? Any practical example for this?
I know a bit, Hashmap used to work based on this - Key and values used to put in buckets also bucket has unique numbers. So that, can identify and get the key and value from the buckets. When I put the key/value pair in the bucket of which identifier is the hash code of the key.
For an eg: Hash code of the key is 101 so it is stored in bucket 101. One bucket can store more than key and value pairs. Suppose take an example as Object1 is "A", object2 is "A"and object3 is "B" then it has a same Hash code. So, it stores the different objects by sharing the same Hashcode in same bucket. My doubt is, objects with same Hashcode should be equal and different objects should have different Hashcodes?
This is the program using HashSet:
import java.util.*;
public class Simple{
public static void main(String[] args){
HashSet hh=new HashSet();
hh.add("D");
hh.add("A");
hh.add("B");
hh.add("C");
hh.add("a");
System.out.println("Checking the size is:"+hh.size()+"");
System.out.println(hh);
Iterator i=hh.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
}
}
Output is,
Checking the size is:5
[D, A, B, a, C]
D
A
B
a
C
My doubt is, why "a" is inserting in between "B" and "C".
Now, I am using LinkedHashSet so,
public class Simple{
public static void main(String[] args){
LinkedHashSet hh=new LinkedHashSet();
hh.add("D");
hh.add("A");
hh.add("B");
hh.add("C");
hh.add("a");
System.out.println("Checking the size is:"+hh.size()+"");
System.out.println(hh);
Iterator i=hh.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
}
}
I just understand that, it follows insertion order and it avoids duplicate elements. So the output is,
Checking the size is:5
[D, A, B, C, a]
D
A
B
C
a
Now, Using Treeset:
import java.util.*;
public class Simple{
public static void main(String[] args){
TreeSet hh=new TreeSet();
hh.add("1");
hh.add("5");
hh.add("3");
hh.add("5");
hh.add("2");
hh.add("7");
System.out.println("Checking the size is:"+hh.size()+"");
System.out.println(hh);
Iterator i=hh.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
}
}
Here, I just understand that - Treeset follows ascending order.
The output is,
Checking the size is:5
[1, 2, 3, 5, 7]
1
2
3
5
7
Then my doubt is, how does Hashset works in Java? And I know that LinkedHashset follows doubly linkedlist. If it uses doubly linked list then how does it stores the elements? What does mean by doubly linkedlist and how does it works? Then where all these three Hashset, Treeset, LinkedHashset would be used in Java and which one has better performance in Java?
See Question&Answers more detail:
os