hashset

HashSet with Index Access

◇◆丶佛笑我妖孽 提交于 2019-11-30 21:22:38
问题 I would need a data structure that Allows me to add/item to it Do not allow duplication access the collection via index I am thinking about hashset, but HashSet doesn't have an index. What is the data structure that fulfills the above need? 回答1: How about a collection derived from KeyedCollection<TKey, TItem>? This represents a collection of items where each key is derived from the item itself. By default it does not allow you to add duplicates (i.e. items with the same key). It allows lookup

What happens to the lookup in a Hashmap or Hashset when the objects Hashcode changes

倖福魔咒の 提交于 2019-11-30 19:14:52
In a Hashmap the hash code of the key provided is used to place the value in the hashtable. In a Hashset the obects hashcode is used to place the value in the underlying hashtable. i.e the advantage of the hashmap is that you have the flexibility of deciding what you want as the key so you can do nice things like this. Map<String,Player> players = new HashMap<String,Player>(); This can map a string such as the players name to a player itself. My question is is what happens to to the lookup when the key's Hashcode changes. This i expect isn't such a major concern for a Hashmap as I wouldn't

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

[亡魂溺海] 提交于 2019-11-30 16:46:52
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 a subset (not proper) of the other. I think the only way that can happen is if they are equal sets. Example code: HashSet<int> set1 = new HashSet<int>(); set1.Add(1); set1.Add(2); set1.Add(3); HashSet<int> set2 = new HashSet<int>(); set2.Add(1); set2.Add(2);

The difference between 'HashSet' and 'Set' in Scala?

只谈情不闲聊 提交于 2019-11-30 16:41:25
问题 I'm very confused by Scala's HashSet and Set types as they both seem to do the same thing. What is the difference between them? Is it the same in Java? In my reference it says that HashSet is an "explicit set class" (as compared to Set ). What does that mean? 回答1: Scala's mutable and immutable HashSet implementations are concrete classes which you can instantiate. For example, if you explicitly ask for a new scala.collection.immutable.HashSet , you will always get a set which is implemented

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

我的梦境 提交于 2019-11-30 16:21:53
问题 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 a subset (not proper) of the other. I think the only way that can happen is if they are equal sets. Example code: HashSet<int> set1 = new HashSet<int>(); set1

Why HashSet<T> class is not used to implement Enumerable.Distinct

廉价感情. 提交于 2019-11-30 16:10:09
I needed to access the asymptotic time and space complexity of the IEnumerable.Distinct in big O notation So I was looking at the implementation of extension method Enumerable.Distinct and I see it is implemented using and internal class Set<T> , which is almost a classical implementation of a hash table with "open addressing" What quickly catches the eye is that a lot of code in Set<T> is just a copy-paste from HashSet<T> , with some omissions However, this simplified Set<T> implementation has some obvious flaws, for example the Resize method not using prime numbers for the size of the slots,

Java8 Stream over a set consistency of the order

夙愿已清 提交于 2019-11-30 15:24:20
From what i understand, Set in java is an unordered collection and an iterator will process the items in some certain order of its choice(I might be wrong here) but ensures it process all the elements in the set. In Java8, stream() API in Collections has been introduced with skip and limit functionality. So i want to know if the order of the items processed from stream remain same irrespective of how many times i start a stream or will it be random everytime ? Will the order change if the set gets modified in between the streams ? May be irrelevant but i am providing the problem here : Now

Is there a HashSet in Delphi?

北城以北 提交于 2019-11-30 13:45:49
问题 Is there a HashSet in Delphi? I know using set can at most hold 255 items. Is there a HashSet in latest Delphi Compiler e.g. XE8, Seattle 回答1: The standard collections do not offer a generic set class. Third party collections libraries such as Spring4D do. You can build a generic set class quite easily on top of TDictionary<K, V> . A bare bones version might look like this: type TSet<T> = class private FDict: TDictionary<T, Integer>; public constructor Create; destructor Destroy; override;

can StringBuffer objects be keys in TreeSet in Java?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 09:47:32
问题 I have the following code where I am trying to put the StringBuffer objects as keys in a TreeSet. The reason I do this is to see if I can put mutable objects as keys. I do not get any compile error. but when I run this code, I get the error that is below the code. specially, I get this java.lang.StringBuffer cannot be cast to java.lang.Comparable . what does this error indicate? from javadoc I see that StringBuffer class is declared final ( public final class StringBuffer ), doesn't that mean

Is Contains thread safe in HashSet<T>

允我心安 提交于 2019-11-30 09:00:06
问题 Looking at the code for Contains in the HashSet<T> class in the .NET source code, I cannot find any reason why Contains is not thread safe? I am loading a HashSet<T> with values ahead of time, and then checking Contains in a multi threaded . AsParallel() loop. Is there any reason why this would not be safe. I am loath to use ConcurrentDictionary when I don't actually require storing values. 回答1: Normally ( normally ) collections that are used only for reading are "unofficially" thread safe