Saw the code snippet like
Set<Record> instances = new HashSet<Record>();
I am wondering if Hashset is a special kind of set. Any difference between them?
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.
A
Setrepresents a generic “set of values”. ATreeSetis a set where the elements are sorted (and thus ordered), aHashSetis a set where the elements are not sorted or ordered.A
HashSetis typically a lot faster than aTreeSet.A
TreeSetis typically implemented as a red-black tree (See http://en.wikipedia.org/wiki/Red-black_tree – I’ve not validated the actual implementation of sun/oracle’sTreeSet), whereas aHashSetusesObject.hashCode()to create an index in an array. Access time for a red-black tree isO(log(n))whereas access time for aHashSetranges from constant-time to the worst case (every item has the same hashCode) where you can have a linear search timeO(n).