Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Hashset - Sorting of strings

829294Jan 7 2011 — edited Jan 7 2011
Hi,

I have tried the below code


public class TestSet {
public static void main(String[] args) {
HashSet<String> s=new HashSet<String>();
s.add("Z");
s.add("C");
s.add("X");
s.add("A");
s.add("B");
Iterator i=s.iterator();
while(i.hasNext()){
System.out.println((String)i.next());
}
}
}

As per API HashSet is unsorted so i should get the result ZCXAB, instead i got printed ABCXZ how come sorting is happened automatically?

Appriciate any suggestions on this?
This post has been answered by tschodt on Jan 7 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 4 2011
Added on Jan 7 2011
9 comments
882 views