Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashset

 
weiliu lili
Ranch Hand
Posts: 46
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic