hashset

HashSet problem -- equals and hashCode with contains working differently than I'd expect

I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj): return self.tag.equals(obj.tag) def hashCode(self): return self.tag.hashCode() from java.lang import String from java.util import HashMap from java.util import HashSet tag1 = IncidentTag(Str...

Java: Trouble with Generics & Collection type detection

I have a class called DataSet with various constructors, each specifying a different type of variable. It might look a bit like this: public class DataSet { private HashSet Data; public DataSet( DataObject obj ) { Data = new <DataObject>HashSet(); Data.add( obj ); } public DataSet( ObjectRelations...

How do you determine if two HashSets are equal (by value, not by reference)?

I am trying to determine if two HashSet objects in .NET 3.5 (C#) are equal sets, i.e. contain the same values. This seems like something one would obviously want to do but none of the provided functions seem to give you this information. The way I can think to do this is by checking if the count of the two sets are equal and one set is ...

Modifying a set during iteration java

I'm looking to make a recursive method iterative. I have a list of Objects I want to iterate over, and then check their subobjects. Recursive: doFunction(Object) while(iterator.hasNext()) { //doStuff doFunction(Object.subObjects); } I want to change it to something like this doFunction(Object) iIterator = hashSet.iterator(); ...

HashSet performance Add vs Contains for existing elements

Frustratingly this question just got closed: So I will put it here in its correct form: For some reason, it seems the Add operation on a HashSet is slower than the Contains operation when the element already exists in the HashSet. Here is proof: Stopwatch watch = new Stopwatch(); int size = 10000; int iterations = 100...

Does HashSet preserve insertion order?

Does the HashSet collection introduced in .NET 3.5 preserve insertion order when iterated using foreach? The documentation states, that the collection is not sorted, but it doesn't say anything about insertion order. A pre-release BCL blog entry states that it is unordered, but this article states that it is designed to preserve insert...

Why is HashSet<T>.IsReadOnly explicit?

This var h = new HashSet<int>(); var r = h.IsReadOnly; does not compile. I have to do var r = ((ICollection<int>)h).IsReadOnly; why wasn't IsReadOnly implemented normally? (I'm not asking how, but why) ...

Remove from a HashSet failing after iterating over it

I'm writing an agglomerative clustering algorithm in java and having trouble with a remove operation. It seems to always fail when the number of clusters reaches half the initial number. In the sample code below, clusters is a Collection<Collection<Integer>>. while(clusters.size() > K){ // determine smallest distance b...

Is there a way to get the difference between two sets of objects in c#

I want to get the difference between two sets of ints in c#. Given s1 and s2 I want to return those ints which are in s1 and not in s2. I can do something such as: List<int> s1 = new List<int>(); List<int> s2 = new List<int>(); foreach (int i in s1) { if (s1.Contains(i)) { // } ...

Using HashSet<T> in C# Properties.Settings

I am creating some C# tools for the game I am working on, and I'm adding the "recent files->" drop down menu. The problem is, I can't get C#'s "Settings" page in VS2008 to allow me to add a typed HashSet. There's just no option to do it. I got all hackalicious and manually edited the "Settings.Designer.cs" file to: [global::System...

HashSet in WCF

hi friends, i'm using a HashSet in my WCF interface [ServiceContract] public interface IwcfServerSync { [OperationContract] void Test(HashSet<int> someHashSet); } When i create a service reference the HashSet turns into a int[]. I added a ServiceKnownType : [ServiceKnownType(typeof(System.Collections.Generic.HashSet<int>))...

How do I control how an object is hashed by a hashset

I'm using a HashSet<T> to store a collection of objects. These objects already have a unique ID of System.Guid, so I'd rather the HashSet<> just use that existing ID rather then trying to figure out itself how to hash the object. How do I override the build in hashing and force my program to use the build in ID value as the hash value? ...

HashSet IntersectWith Count words but only unique

Hi , i got richtextBox control in form and a text file. I am getting text file to array and getting richtextbox1.text to other array than compare it and count words matching. But for example there are two "name" word in text file and three "and" word in richtextbox .. So if there is two same word in text file in richtextbox it cant be 3 ...

Bind HashSet to a ListView Item (C#, WPF)

Hello. I'm trying to bind a HashSet to a ListView item. I've documented my code here: public class Person { public string Name { get; set; } public AddressList = new AddressList (); } public class AddressList : HashSet<Addresses> { // } public class Addresses { public string Streetname { get; set; } public string Ci...

How to use Comparer for a HashSet

As a result of another question I asked here I want to use a HashSet for my objects I will create objects containing a string and a reference to its owner. public class Synonym { private string name; private Stock owner; public Stock(string NameSynonym, Stock stock) { name=NameSynonym; owner=stock } //...

Why have HashSet but not Set in C#?

Old question My understanding is that C# has in some sense HashSet and set types. I understand what HashSet is. But why set is a separate word? Why not every set is HashSet<Object>? New question Why does C# has no generic Set type, similar to Dictionary type? From my point of view, I would like to have a set with standard lookup/addit...

Collection lookups; alternative to Dictionary

Hello: A TimeSheetActivity class has a collection of Allocations. An Allocation is a value object that is used by other objects in the domain as well, looking something like this: public class Allocation : ValueObject { public virtual StaffMember StaffMember { get; private set; } public virtual TimeSheetActivity Activity { get;...

Ran into issues while implementing hash table

I am trying to read a file which contains URLs and are 100 million in number. What I need to do is find out the different websites they are from. So, I am taking chunk of data in memory and reading it line by line. Also, I need to find out how many URLs does each website has in the file and what are those URLs. The way I figured it out i...

Remove Elements from a HashSet while Iterating

So, if I try to remove elements from a Java HashSet while iterating, I get a ConcurrentModificationException. What is the best way to remove a subset of the elements from a HashSet as in the following example? Set<Integer> set = new HashSet<Integer>(); for(int i = 0; i < 10; i++) set.add(i); // Throws ConcurrentModificationExcept...

Java: Collection RemoveAll Ignoring Case?

Ok so here is my issue. I have to HashSet's, I use the Removeall method to delete values that exist in one set from the other. Prior to calling the method, I obviously add the values to the Sets. I call .toUpperCase() on each String before adding because the values are of different cases in both lists. There is no rhyme or reason to th...