differences between set implemented classes

Hi,

what are main differences between set implemented classes like HashSet, LinkedHAsSet, TreeSet.

How the Null is allowed any where in first two and allowed only as first element in other.

Please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
HashSet - stores elements with no ordering information.  The only question you can ask is "is X in the set?"

LinkedHashSet - stores elements together with the order they were added.  You can ask "is X in the set and what was the 2nd element added to the set?"

TreeSet - stores elements and keep them sorted.  You can ask "is X in the set and what is the first element in order in the set"

So you should use:
a) HashSet when you don't care about the order of the elements
b) LinkedHashSet when you care about the order the elements were added
c) TreeSet when you want to keep the elements sorted (e.g. in alphabetic order)

Does that make sense?

Doug
gudii9Author Commented:
That is more clear now.


How the Null is allowed any where in first two(HashSet,, LinkedHashSet) and allowed only as first element in other(TreeSet).

can you please advise on above
dpearsonCommented:
You can add a null to any of them.  The problem with TreeSet is that it relies on a Comparator to sort the elements in the set (see the earlier answer above this one - it's the only one that keeps the elements sorted).

So to add a null to a TreeSet the Comparator has to know how to compare to null.  You can write a Comparator like that if you want, but the default ones (for Strings, Integers etc.) don't support it.

So null works without any work in HashSet and LinkedHashSet.  But it only works in TreeSet if you write your own Comparator.

Doug
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

gudii9Author Commented:
LinkedHashSet - stores elements together with the order they were added.  You can ask "is X in the set and what was the 2nd element added to the set?"

TreeSet - stores elements and keep them sorted.  You can ask "is X in the set and what is the first element in order in the set"

cannot i ask below question to TreeSet as well?  why not
You can ask "is X in the set and what was the 2nd element added to the set?"
gudii9Author Commented:
List supports duplicates so preserves natural order

Set does not support duplicates so natural order wont be preserve natural order
HashSet does not support duplicates so natural order wont be preserve natural order

TreeSet since we implement comparator can control which basis to sort ie supports custom sorting order right  like based on FirstName, LastName of Customer object. If String just acending alphabetical sorting order right



I was not clear on LinkedHasSet yet though.

Map has key value pairs  to search faster based on the key rather than looking at all indexes similar to List and Set

hashtable(not thread safe) ,properties(not thread safe) and HashMap(thread safe) implemtations of Map which honor above Map rules.

Please correct me if i am work
dpearsonCommented:
cannot i ask below question to TreeSet as well?  why not
You can ask "is X in the set and what was the 2nd element added to the set?"

No you can't ask TreeSet this question.

There are 2 orderings involved here.  One is the order elements are added.  LinkedHashSet records that.
The second ordering is the natural ordering (think alphabetical).  TreeSet uses that one.

In case the difference isn't clear, let's do this without code.
Add this sequence: "A", "Z","G","D"

What was 2nd element added?  "Z"    <-- LinkedHashSet can give you this
What is 2nd element in order? "D"      <-- TreeSet can give you this

Is that clear now?

Doug

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
HashSet does not preserve insertion order where as the LinkedHashSet preserves it right. TreeSet keeps natural ordering(alphabetical ascending for string)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.