Possible Duplicate:
Hashset vs Treeset
Can you use HashSet and TreeSet interchangeably? If I exchanged TreeSet for Hashset and vice versa in a program what issues would there be? Im aware you need to implement Comparable for a TreeSet.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If some API requires
Set, it absolutely doesn’t matter which implementation you pass. If it requires concrete type (unlikely), you can’t mix them.In general, they difference is in performance (
HashSetis faster) but this shouldn’t affect how your program behaves and in order. Order of items inHashSetis unpredictable. If your program relies on any such order, it should rather useLinkedHashSetorTreeSet.