This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Um.. yeah but how come your getting A,C,B. Shouldn't you be getting C,A,B, the order which they were inserted?
Thanks.
burak cetin
Greenhorn
Joined: Jul 09, 2008
Posts: 3
posted
0
Hashset is neither ordered nor sorted. so when you access the variables the outcome is unpredictable. there was a good explanation of the difference between ordered and sorted in kate&bert.
BC [ July 25, 2008: Message edited by: burak cetin ]
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 622
posted
0
Um.. sorted means 1,2,3,4 etc.. or a,b,c,d etc.. whats ordered?
Thanks.
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
posted
0
Originally posted by Arjun Reddy: Um.. sorted means 1,2,3,4 etc.. or a,b,c,d etc.. whats ordered?
Thanks.
Ordered means in what order like (First In First Out) or (Last In First Out) or any other way of inserting and retrieving from the Collection. But Set is not ordered, means it does not have any order of the above avtivities. If you get so called sorted output (a,b,c) its just a chance that for you it was like that (to confuse you more)
cmbhatt
Deepak Bajoria
Ranch Hand
Joined: Mar 07, 2008
Posts: 35
posted
0
I think it all depends on the hash function used by the JVM. Like for e.g if a hashCode() simply returns the ascii value of the letter, the output would be sorted
Originally posted by Deepak Bajoria: I think it all depends on the hash function used by the JVM. Like for e.g if a hashCode() simply returns the ascii value of the letter, the output would be sorted
Not really. First, it depends on how the buckets are implemented. The Set may not map linearly with hashcode. Second, there may not be a one to one mapping between buckets and hashcodes. Two different letter may end up in the same bucket, then it will depend on how each bucket is implemented.
One other point, if something is not sorted or ordered, it doesn't mean that the ordering random. Don't expect iterations through the Set to yield different results -- it may, but it may not. The point is, any ordering is purely coincidental, and the HashSet collection has not been implemented with any ordering.
Henry
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 622
posted
0
Thank you guys.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.