hashset

C# - defining hashset with custom key

牧云@^-^@ 提交于 2019-11-30 08:49:08
I am using the HashSet and Dictionary in C# to implement a Graph structure. I have a problem with the uniqueness of HashSet elements when the HashSet key is a customized class. Here I have: public class Point { public int x { get; set; } public int y { get; set; } } public class Vertex { public Vertex(Point point) { VertexLabel = point; } public Point VertexLabel { get; private set; } } public class Edge { public Edge(Vertex to, Vertex from, double weight) { FromVertex = from; ToVertex = to; Weight = weight; } public Vertex FromVertex { get; private set; } public Vertex ToVertex { get; private

Is there a HashSet in Delphi?

泪湿孤枕 提交于 2019-11-30 08:29:25
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 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; function Contains(const Value: T): Boolean; procedure Include(const Value: T); procedure Exclude(const Value:

Java all determine elements are same in a list

旧街凉风 提交于 2019-11-30 05:56:54
I am trying to determine to see if all elements in a list are same. such as: (10,10,10,10,10) --> true (10,10,20,30,30) --> false I know hashset might be helpful, but i don't know how to write in java. this is the one I've tried, but didn't work: public static boolean allElementsTheSame(List<String> templist) { boolean flag = true; String first = templist.get(0); for (int i = 1; i< templist.size() && flag; i++) { if(templist.get(i) != first) flag = false; } return true; } Using the Stream API (Java 8+) boolean allEqual = list.stream().distinct().limit(2).count() <= 1 or boolean allEqual = list

Changing values in HashSet

妖精的绣舞 提交于 2019-11-30 05:24:31
I've read this question: Changing the elements in a set changes the 'equals' semantics However, I don't know how to solve the problem that I can't change an item in the HashSet and remove it later. I have some example sourcecode: public static void main(String[] args) { TestClass testElement = new TestClass("1"); Set<TestClass> set = new HashSet<>(); set.add(testElement); printIt(testElement, set, "First Set"); testElement.setS1("asdf"); printIt(testElement, set, "Set after changing value"); set.remove(testElement); printIt(testElement, set, "Set after trying to remove value"); testElement

What is the JavaScript equivalent to a C# HashSet?

对着背影说爱祢 提交于 2019-11-30 04:10:48
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) 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:true //etc... }; var checkValue = function(value){ return hash[value] === true; }; checkValue(7); // => true

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

删除回忆录丶 提交于 2019-11-30 03:00:35
问题 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

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

一曲冷凌霜 提交于 2019-11-30 00:33:10
问题 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)) { // } else { // } } But I was wondering if anyone can point out anything cleaner. I would like to do something such as List<int> omitted = s1.Difference(s2); Not sure if there is an existing method or a LINQ construct that

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

依然范特西╮ 提交于 2019-11-29 23:36:58
问题 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

Java8 Stream over a set consistency of the order

故事扮演 提交于 2019-11-29 22:35:34
问题 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

can StringBuffer objects be keys in TreeSet in Java?

送分小仙女□ 提交于 2019-11-29 16:44:01
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 it is immutable and hence hashable? I am a newbie to the hashing and immutable stuff, so kindly help