Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enumerate hashset and delete elements from it

Tags:

c#

linq

hashset

I want to go through a HashSet and make a (complicated) check on each element which results in saving the element, removing the element from the HashSet or doing nothing.

Since a foreach loop does not allow me to change the HashSet and an index is not available, I do not know how to perform the task (without doing slow things like copying the HashSet first or applying several LINQ operations which means enumerating the HashSet more than once).

Any suggestions?

like image 638
J Fabian Meier Avatar asked Apr 02 '13 08:04

J Fabian Meier


People also ask

How do I remove an element from a HashSet?

HashSet remove() method is used to remove a particular element from a HashSet.

How do I remove an element from a Set in Java 8?

Set remove() method in Java with Examplesutil. Set. remove(Object O) method is used to remove a particular element from a Set.

How do I remove the first element from a HashSet?

To remove a single element from a HashSet, use the remove() method.

How do I remove one element from a collection?

An element can be removed from a Collection using the Iterator method remove(). This method removes the current element in the Collection. If the remove() method is not preceded by the next() method, then the exception IllegalStateException is thrown.


1 Answers

You just need to use RemoveWhere() with an appropriate predicate function.

You can have a side-effect of your predicate which copies the element being checked (assuming that's what you mean by "save the element"), if needed. Perhaps that sounds a little hacky, but it would work fine I think.

This works because your predicate function will be presented with each element of the HashSet in an undefined order, and therefore you can decide what to do with each element, as well as returning true to delete it and false to keep it.

[EDIT] Here's a code sample.

using System;
using System.Collections.Generic;

namespace Demo
{
    public class Program
    {
        [STAThread]
        private static void Main(string[] args)
        {
            var hashSet = new HashSet<int> {4, 0, 6, -1, 23, -8, 14, 12, -9, 5, 2};
            var itemProcessor = new ItemProcessor();

            hashSet.RemoveWhere(itemProcessor.Process);

            Console.WriteLine("Max = {0}, Min = {1}", itemProcessor.Max, itemProcessor.Min);
            Console.WriteLine("\nHashSet contents:");

            foreach (int number in hashSet)
            {
                Console.WriteLine(number);
            }
        }
    }

    public sealed class ItemProcessor
    {
        private int max = int.MinValue;
        private int min = int.MaxValue;

        // Removes all negative numbers and calculates max and min values.

        public bool Process(int item)
        {
            max = Math.Max(item, max);
            min = Math.Min(item, min);

            return (item < 0);
        }

        public int Max { get { return max; } }
        public int Min { get { return min; } }
    }
}
like image 187
Matthew Watson Avatar answered Sep 30 '22 17:09

Matthew Watson