Thanks Naveen for the explanation. If somebody is wondering why the hash set displays the integers in sorted order then that's because hash set uses hash map internally and hash map stores the elements in a hash table. Since we are adding integers it uses integers hash code which returns an integer. So if you add integers they are added as per sorted order. However if you try adding double values then you would see a rather different output as the hash code computation here is slightly different. Set st = new HashSet(); st.add(2.4); st.add(1.4); st.add(3.4); st.add(4.4); st.add(5.4); st.add(6.4); st.add(7.4); st.add(8.4); for (Double i : st) { System.out.println(i);} Output:- 2.4 1.4 6.4 7.4 3.4 4.4 8.4 5.4
Hi Can you please make videos for front tools using java/jsp .May be it will help u so many resource to participate in events occurring in company so that there is no dependency on other teams
In ur previous java collection series u taught us that hashset directly extends set interface and tree set extends sorted set and now I am confused between these two videos
Thanks Naveen for the explanation. If somebody is wondering why the hash set displays the integers in sorted order then that's because hash set uses hash map internally and hash map stores the elements in a hash table. Since we are adding integers it uses integers hash code which returns an integer. So if you add integers they are added as per sorted order. However if you try adding double values then you would see a rather different output as the hash code computation here is slightly different.
Set st = new HashSet();
st.add(2.4);
st.add(1.4);
st.add(3.4);
st.add(4.4);
st.add(5.4);
st.add(6.4);
st.add(7.4);
st.add(8.4);
for (Double i : st) {
System.out.println(i);}
Output:-
2.4
1.4
6.4
7.4
3.4
4.4
8.4
5.4
Thanks you . All video well explained. i have switched job after watching u r learning video .
All the best :)
Thank you
Hi Can you please make videos for front tools using java/jsp .May be it will help u so many resource to participate in events occurring in company so that there is no dependency on other teams
When i add like this s.addAll(Arrays.asList(new int[] {3,5,6})); i am getting the below error:
The method addAll(Collection
try new Integer[] instead of new int[] as Collection stores objects and not primitives
In ur previous java collection series u taught us that hashset directly extends set interface and tree set extends sorted set and now I am confused between these two videos
It's an advanced series. Refer the latest one
@@naveenautomationlabs in latest one also... Hashset directly implementing set interface and tree set implementing sorted set