Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select an element by index from a .NET HashSet

Tags:

c#

.net

hashset

At the moment I am using a custom class derived from HashSet. There's a point in the code when I select items under certain condition:

var c = clusters.Where(x => x.Label != null && x.Label.Equals(someLabel));

It works fine and I get those elements. But is there a way that I could receive an index of that element within the collection to use with ElementAt method, instead of whole objects?

It would look more or less like this:

var c = select element index in collection under certain condition;
int index = c.ElementAt(0); //get first index
clusters.ElementAt(index).RunObjectMthod();

Is manually iterating over the whole collection a better way? I need to add that it's in a bigger loop, so this Where clause is performed multiple times for different someLabel strings.

Edit

What I need this for? clusters is a set of clusters of some documents collection. Documents are grouped into clusters by topics similarity. So one of the last step of the algorithm is to discover label for each cluster. But algorithm is not perfect and sometimes it makes two or more clusters with the same label. What I want to do is simply merge those cluster into big one.

like image 874
Ventus Avatar asked Sep 30 '10 08:09

Ventus


People also ask

Does HashSet have Index C#?

A HashSet<T> stores a set of distinct values in no particular order. Unlike a List<T> , a HashSet<T> doesn't have any index which can be used to store and retrieve an element in constant time.

Are Hashsets indexed?

In HashSet you won't be able to find value by index. If you still want to find the element you have no other option but to use the iterator, it will iterate through the Hashset and give you one by one element from the Hashset. You want to find your element by index you should use ArrayList.

What is .NET HashSet?

A HashSet is an optimized collection of unordered, unique elements that provides fast lookups and high-performance set operations. The HashSet class was first introduced in . NET 3.5 and is part of the System. Collection.


2 Answers

Sets don't generally have indexes. If position is important to you, you should be using a List<T> instead of (or possibly as well as) a set.

Now SortedSet<T> in .NET 4 is slightly different, in that it maintains a sorted value order. However, it still doesn't implement IList<T>, so access by index with ElementAt is going to be slow.

If you could give more details about why you want this functionality, it would help. Your use case isn't really clear at the moment.

like image 194
Jon Skeet Avatar answered Oct 24 '22 18:10

Jon Skeet


In the case where you hold elements in HashSet and sometimes you need to get elements by index, consider using extension method ToList() in such situations. So you use features of HashSet and then you take advantage of indexes.

HashSet<T> hashset = new HashSet<T>();

//the special situation where we need index way of getting elements
List<T> list = hashset.ToList();

//doing our special job, for example mapping the elements to EF entities collection (that was my case)

//we can still operate on hashset for example when we still want to keep uniqueness through the elements 
like image 42
Bronek Avatar answered Oct 24 '22 18:10

Bronek