Hashset question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hashset question

why order is wrong for the output?I thought it is in alphabetical order? https://code.sololearn.com/cVaETg59mVn0/?ref=app

7th Nov 2017, 9:16 AM
oyl
7 Answers
+ 12
A hash is generated for every element in a hashset. The elements are displayed based on their "hashes" but not in alphabetical order.
7th Nov 2017, 9:25 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 2
Hashes are calculated by some algorithm. It depends on that algorithm whether you get the same order as you would with alphabetical ordering.
7th Nov 2017, 9:31 AM
OWRhcmFidQ==
+ 2
they are explicitly saying if you want to keep order to use a LinkedHashSet
7th Nov 2017, 9:40 AM
Jeremy
Jeremy - avatar
0
Because it's ordered by the objects hash code and not by their value.
7th Nov 2017, 9:25 AM
OWRhcmFidQ==
0
as others have pointed out, hashsets are sorted after the objects hash. You can use an Arraylist and Collections.sort if you need it to be sorted in other ways.
7th Nov 2017, 9:27 AM
Jeremy
Jeremy - avatar
0
so it only works on characters like A,B,C???
7th Nov 2017, 9:29 AM
oyl
0
when im reading the comments from the hashset lesson in sololearn.why do they say it is in alphabetical order.so they are wrong?
7th Nov 2017, 9:37 AM
oyl