Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fastest/safest method to iterate over a HashSet?

I'm still quite new to C#, but noticed the advantages through forum postings of using a HashSet instead of a List in specific cases.

My current case isn't that I'm storing a tremendous amount of data in a single List exectly, but rather than I'm having to check for members of it often.

The catch is that I do indeed need to iterate over it as well, but the order they are stored or retrieved doesn't actually matter.

I've read that for each loops are actually slower than for next, so how else could I go about this in the fastest method possible?

The number of .Contains() checks I'm doing is definitely hurting my performance with lists, so at least comparing to the performance of a HashSet would be handy.

Edit: I'm currently using lists, iterating through them in numerous locations, and different code is being executed in each location. Most often, the current lists contain point coordinates that I then use to refer to a 2 dimensional array for that I then do some operation or another based on the criteria of the list.

If there's not a direct answer to my question, that's fine, but I assumed there might be other methods of iterating over a HashSet than just foreach cycle. I'm currently in the dark as to what other methods there might even be, what advantages they provide, etc. Assuming there are other methods, I also made the assumption that there would be a typical preferred method of choice that is only ignored when it doesn't suite the needs (my needs are pretty basic).

As far as prematurely optimizing, I already know using the lists as I am is a bottleneck. How to go about helping this issue is where I'm getting stuck. Not even stuck exactly, but I didn't want to re-invent the wheel by testing repeatedly only to find out I'm already doing it the best way I could (this is a large project with over 3 months invested, lists are everywhere, but there are definitely ones that I do not want duplicates, have a lot of data, need not be stored in any specific order, etc).

like image 340
Mythics Avatar asked Mar 08 '12 21:03

Mythics


People also ask

How to iterate HashSet in Java?

First, we make an iterator to iterate HashSet with the help of the iterator () method in Java. And then iterate HashSet with the help of hasNext () and Next () method in Java. Where hasNext () method check whether HashSet has elements or not and, Next () method access the data of HashSet.

What are the methods in HashSet?

Methods in HashSet METHOD DESCRIPTION iterator () Used to return an iterator over the elem ... isEmpty () Used to check whether the set is empty o ... size () Used to return the size of the set. clone () Used to create a shallow copy of the set ... 4 more rows ...

How to improve performance of HashSet in Java?

HashSet does not have an indexer so you have to use the enumerator. In this case foreach is efficient as it only calls MoveNext () as it moves through the collection. Also Parallel.ForEach can dramatically improve your performance, depending on the work you are doing in the loop and the size of your HashSet.

What is the fastest way to iterate through a list?

Iterating is fast as you can use the List, checking for Contains is fast as you can use the HashSet. Just make it an IEnumerable and you can use this Collection in foreach as well.


1 Answers

You shouldn't be iterating over a hashset in the first place to determine if an item is in it. You should use the HashSet (not the LINQ) contains method. The HashSet is designed such that it won't need to look through every item to see if any given value is inside of the set. That is what makes it so powerful for searching over a List.

like image 89
Servy Avatar answered Sep 27 '22 18:09

Servy