hashset

Why have HashSet but not Set in C#?

佐手、 提交于 2019-11-29 05:29:06
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/addition/deletion performance. I wouldn't care much whether it is realized with hashes or something else. So why not make a set class that would actually be implemented as a HashSet in this version of C# but perhaps somewhat different in a future version? Or why not

Contains of HashSet<Integer> in Python

让人想犯罪 __ 提交于 2019-11-29 02:50:57
In Java we have HashSet<Integer> , I need similar structure in Python to use contains like below: A = [1, 2, 3] S = set() S.add(2) for x in A: if S.contains(x): print "Example" Could you please help? Just use a set: >>> l = set() >>> l.add(1) >>> l.add(2) >>> 1 in l True >>> 34 in l False The same works for lists: >>> ll = [1,2,3] >>> 2 in ll True >>> 23 in ll False Edit: Note @bholagabbar's comment below that the time complexity for in checks in lists and tuples is O(n) on average (see the python docs here ), whereas for sets it is on average O(1) (worst case also O(n), but is very uncommon

C# Hashset Contains Non-Unique Objects

拜拜、爱过 提交于 2019-11-29 01:28:12
Using this class public class Foo { public string c1, c2; public Foo(string one, string two) { c1 = one; c2 = two; } public override int GetHashCode() { return (c1 + c2).GetHashCode(); } } And this HashSet HashSet<Foo> aFoos = new HashSet<Foo>(); Foo aFoo = new Foo("a", "b"); aFoos.Add(aFoo); aFoos.Add(new Foo("a", "b")); label1.Text = aFoos.Count().ToString(); I get the answer 2, when surely it should be 1. Is there a way to fix this so my HashSet contains only unique objects? Thanks, Ash. The HashSet<T> type ultamitely uses equality to determine whether 2 objects are equal or not. In the

What causes the slightly unpredictable ordering of the iterator() for the java.util.HashSet and HashMap.keySet() classes?

前提是你 提交于 2019-11-28 23:49:28
Six years ago, I burned several days trying to hunt down where my perfectly deterministic framework was responding randomly. After meticulously chasing the entire framework ensuring that it was all using the same instance of Random, I then kept chasing by single stepping code. It was highly repetitive iterative self-calling code. Worse, the damn effect would only show up after a huge number of iterations were completed. And after +6 hours, I was finally at wits end when I discovered a line in the javadoc for HashSet.iterator() indicating it doesn't guarantee the order in which it will return

ordering a hashset example?

删除回忆录丶 提交于 2019-11-28 23:10:47
I need an example on how to use a comparable class on a HashSet to get an ascending order. Let’s say I have a HashSet like this one: HashSet<String> hs = new HashSet<String>(); How can I get hs to be in ascending order? Use a TreeSet instead. It has a constructor taking a Comparator . It will automatically sort the Set . If you want to convert a HashSet to a TreeSet , then do so: Set<YourObject> hashSet = getItSomehow(); Set<YourObject> treeSet = new TreeSet<YourObject>(new YourComparator()); treeSet.addAll(hashSet); // Now it's sorted based on the logic as implemented in YourComparator. If

How to use HashSet<string>.Contains() method in case -insensitive mode?

落爺英雄遲暮 提交于 2019-11-28 19:03:34
How to use HashSet<string>.Contains() method in case -insensitive mode? You can create the HashSet with a custom comparer: HashSet<string> hs = new HashSet<string>(StringComparer.OrdinalIgnoreCase); hs.Add("Hello"); Console.WriteLine(hs.Contains("HeLLo")); Kobi You need to create it with the right IEqualityComparer : HashSet<string> hashset = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase); You should use the constructor which allows you to specify the IEqualityComparer you want to use. HashSet<String> hashSet = new HashSet<String>(StringComparer.InvariantCultureIgnoreCase); The

Collection that allows only unique items in .NET?

喜夏-厌秋 提交于 2019-11-28 18:30:22
Is there a collection in C# that will not let you add duplicate items to it? For example, with the silly class of public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public override int GetHashCode() { return (FirstName + LastName + Address).GetHashCode(); } public override bool Equals(object obj) { Customer C = obj as Customer; return C != null && String.Equals(this.FirstName, C.FirstName) && String.Equals(this.LastName, C.LastName) && String.Equals(this.Address, C.Address); } } The following code will

The HashSet<T>.removeAll method is surprisingly slow

妖精的绣舞 提交于 2019-11-28 16:37:45
Jon Skeet recently raised an interesting programming topic on his blog: "There's a hole in my abstraction, dear Liza, dear Liza" (emphasis added): I have a set – a HashSet , in fact. I want to remove some items from it… and many of the items may well not exist. In fact, in our test case, none of the items in the "removals" collection will be in the original set. This sounds – and indeed is – extremely easy to code. After all, we’ve got Set<T>.removeAll to help us, right? We specify the size of the "source" set and the size of the "removals" collection on the command line, and build both of

Why is HashSet<Point> so much slower than HashSet<string>?

两盒软妹~` 提交于 2019-11-28 15:26:13
I wanted to store some pixels locations without allowing duplicates, so the first thing comes to mind is HashSet<Point> or similar classes. However this seems to be very slow compared to something like HashSet<string> . For example, this code: HashSet<Point> points = new HashSet<Point>(); using (Bitmap img = new Bitmap(1000, 1000)) { for (int x = 0; x < img.Width; x++) { for (int y = 0; y < img.Height; y++) { points.Add(new Point(x, y)); } } } takes about 22.5 seconds. While the following code (which is not a good choice for obvious reasons) takes only 1.6 seconds: HashSet<string> points = new

How is this HashSet producing sorted output?

丶灬走出姿态 提交于 2019-11-28 13:41:58
The following code produces the out put [1,2] even though hashset is not sorted. Set set = new HashSet(); set.add(new Integer(2)); set.add(new Integer(1)); System.out.println(set); Why is that? EDIT: As of Java 8 and later, the following is no longer applicable. This proves that you shouldn't rely on undocumented Java behaviours. This behaviour is caused by several separate reasons: Integers hash to themselves in Java, HashMap s and HashSet s are backed up by an array they also modify hashes using the higher bits to modify the lower bits; if the hash is in range 0..15, it's therefore not