This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashset

 
weiliu lili
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
public class Test {

public static void main(String[] args){
Integer a = new Integer(4);
Integer b = new Integer(8);
Integer c = new Integer(4);
HashSet hs = new HashSet();
hs.add(a);

hs.add(c);
hs.add(b);
System.out.println(hs);
}
}
the running result of it is[8,4] why not [4,8]? how can I enforce the result as [4,8]?
thanks
 
Asif Mahmood
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Weiliu, you're getting unexpected result because hash set does not guarantee the order of its elements because it's based on hashing. If you want to enforce the results, you can use TreeSet.
Best regards,
-Asif
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic