hashset

Why have HashSet but not Set in C#?

你。 提交于 2019-11-27 23:01:42
问题 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

Contains of HashSet<Integer> in Python

≡放荡痞女 提交于 2019-11-27 21:35:11
问题 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? 回答1: 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

What is the JavaScript equivalent to a C# HashSet?

做~自己de王妃 提交于 2019-11-27 20:38:05
问题 I have a list of a few thousand integer keys. The only thing I need to do with this list is say whether or not a given value is in the list. For C# I would use a HashSet to make that look-up fast. What's the JavaScript equivalent? Minimal support level: IE 9+, jQuery (current) 回答1: Under the hood, the JavaScript Object is implemented with a hash table. So, your Key:Value pair would be (your integer):true A constant-time lookup function could be implemented as: var hash = { 1:true, 2:true, 7

HashSet contains problem with custom objects

我怕爱的太早我们不能终老 提交于 2019-11-27 20:12:38
My Custom class that will be contained by HashSet public class Person { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person{" + "hashcode='" + this.hashCode() + '\'' + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Person)) return false; Person person = (Person) o; if (age != person.age) return false; if (!name.equals(person.name)) return false; return true; } @Override public int hashCode() { int result =

How can I convert a Java HashSet<Integer> to a primitive int array?

早过忘川 提交于 2019-11-27 20:04:25
I've got a HashSet<Integer> with a bunch of Integers in it. I want to turn it into an array, but calling hashset.toArray(); returns an Object[] . Is there a better way to cast it to an array of int other than iterating through every element manually? I want to pass the array to void doSomething(int[] arr) which won't accept the Object[] array, even if I try casting it like doSomething((int[]) hashSet.toArray()); Matthew Flaschen Apache's ArrayUtils has this (it still iterates behind the scenes ): doSomething(ArrayUtils.toPrimitive(hashset.toArray())); They're always a good place to check for

How to sort a HashSet?

我是研究僧i 提交于 2019-11-27 18:54:34
For lists, we use the Collections.sort(List) method. What if we want to sort a HashSet ? isak gilbert A HashSet does not guarantee any order of its elements. If you need this guarantee, consider using a TreeSet to hold your elements. However if you just need your elements sorted for this one occurrence, then just temporarily create a List and sort that: Set<?> yourHashSet = new HashSet<>(); ... List<?> sortedList = new ArrayList<>(yourHashSet); Collections.sort(sortedList); Add all your objects to the TreeSet , you will get a sorted Set. Below is a raw example. HashSet myHashSet = new HashSet(

Internal implementation of java.util.HashMap and HashSet

怎甘沉沦 提交于 2019-11-27 18:50:45
I have been trying to understand the internal implementation of java.util.HashMap and java.util.HashSet . Following are the doubts popping in my mind for a while: Whats is the importance of the @Override public int hashcode() in a HashMap/HashSet? Where is this hash code used internally? I have generally seen the key of the HashMap be a String like myMap<String,Object> . Can I map the values against someObject (instead of String) like myMap<someObject, Object> ? What all contracts do I need to obey for this happen successfully? Thanks in advance ! EDIT: Are we saying that the hash code of the

Serializing a HashSet

戏子无情 提交于 2019-11-27 17:12:04
问题 I'm trying to serialize a Hashset but I'm having no luck. Whenever I try to open the serialized data, I get an empty HashSet. However, a List works fine. Example code: [Serializable()] public class MyClass : ISerializable { public MyClass(SerializationInfo info, StreamingContext ctxt) { HashSet<string> hashset = (HashSet<string>)info.GetValue("hashset", typeof(HashSet<string>)); List<string> list = (List<string>)info.GetValue("list", typeof(List<string>)); Console.WriteLine("Printing Hashset:

C# Hashset Contains Non-Unique Objects

痞子三分冷 提交于 2019-11-27 16:08:27
问题 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.

How to use Comparer for a HashSet

我的梦境 提交于 2019-11-27 15:38:40
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 } // [+ 'get' for 'name' and 'owner'] } I understand I need a comparer , but never used it before. Should I create a separate class? like: public class SynonymComparer : IComparer<Synonym> { public int Compare(Synonym One, Synonym Two) { // Should I test if 'One == null' or 'Two == null' ???? return