Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SortedSet<T> vs HashSet<T>

My question is that what is the need of HashSet<T> when we have SortedSet<T>! All HashSet's methods are available in SortedSet too, moreover SortedSet is advantageous as it provides collection already in sorted manner! Even then HashSet is present. For what is it useful then?

like image 408
Batrickparry Avatar asked Jan 07 '11 05:01

Batrickparry


People also ask

What is the difference between HashSet T and List T >?

A HashSet<T> is a class designed to give you O(1) lookup for containment (i.e., does this collection contain a particular object, and tell me the answer fast). A List<T> is a class designed to give you a collection with O(1) random access than can grow dynamically (think dynamic array).

Is HashSet sorted C#?

By definition, a HashSet is not sorted. If you want a sorted hash set, then you should use a SortedSet . The methods it exposes are essentially a superset of those provided by HashSet , including the ability to sort its contents.

Is hash set a sorted set?

Hence sorting of HashSet is not possible. However, the elements of the HashSet can be sorted indirectly by converting into List or TreeSet, but this will keep the elements in the target type instead of HashSet type.


1 Answers

If you don't need sorting, you shouldn't use a class that does sorting because that means your application will be doing more work than it needs to. (It will make your app faster, in other words).

like image 169
Jacob Avatar answered Sep 21 '22 17:09

Jacob