hashset

What's the difference between these two java variable declarations?

别等时光非礼了梦想. 提交于 2019-11-29 16:10:26
public class SomeClass { private HashSet<SomeObject> contents = new HashSet<SomeObject>(); private Set<SomeObject> contents2 = new HashSet<SomeObject>(); } What's the difference? In the end they are both a HashSet isn't it? The second one looks just wrong to me, but I have seen it frequently used, accepted and working. Alan Geleynse Set is an interface, and HashSet is a class that implements the Set interface. Declaring the variable as type HashSet means that no other implementation of Set may be used. You may want this if you need specific functionality of HashSet . If you do not need any

simple C++ hash_set example

冷暖自知 提交于 2019-11-29 14:28:36
问题 I am new to C++ and STL. I am stuck with the following simple example of a hash set storing custom data structures: #include <iostream> #include <ext/hash_set> using namespace std; using namespace __gnu_cxx; struct trip { int trip_id; int delta_n; int delta_secs; trip(int trip_id, int delta_n, int delta_secs){ this->trip_id = trip_id; this->delta_n = delta_n; this->delta_secs = delta_secs; } }; struct hash_trip { size_t operator()(const trip t) { hash<int> H; return H(t.trip_id); } }; struct

C# - defining hashset with custom key

半腔热情 提交于 2019-11-29 12:33:58
问题 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;

What is the main difference between Hashset, Treeset and LinkedHashset, Hashmap and how does it work in Java?

拥有回忆 提交于 2019-11-29 12:24:25
I just understand that LinkedHashSet does not allows duplicate elements when it is inserting. But, I dont understand how does Hashset works in Hava? I know a bit that Hashtable is used in Hashset so the hashtable used to store the elements and here also does not allow the duplicate elements. Then, Treeset is also similar to Hashset it also does not allows duplicate entries so unique elements will be seen and it follows ascending order. I have one more doubt regarding HashMap - Hashmap does not maintains order. It may have one null key and multiple null values. I just dont understand this and

HashSet with two equals object?

女生的网名这么多〃 提交于 2019-11-29 11:39:40
I created an object HashSet, and the value is an object (Triple) which is my own class. But I get a strange thing, when there are two equal objects on my HashSet, is it possible? Here is my overriding method for the equals in the class Triple @Override public boolean equals(Object other){ if (other == null) return false; if (other == this) return true; if (this.getClass() != other.getClass()) return false; Triple otherTriple = (Triple)other; if(otherTriple.getSubject().equals(getSubject()) && otherTriple.getPredicate().equals(getPredicate()) && otherTriple.getObject().equals(getObject()))

Is Contains thread safe in HashSet<T>

99封情书 提交于 2019-11-29 09:25:27
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. Normally ( normally ) collections that are used only for reading are "unofficially" thread safe (there is no collection in .NET that I know that modifies itself during reading). There are some caveats: The

Java HashSet vs Array Performance

别等时光非礼了梦想. 提交于 2019-11-29 09:13:35
问题 I have a collection of objects that are guaranteed to be distinct (in particular, indexed by a unique integer ID). I also know exactly how many of them there are (and the number won't change), and was wondering whether Array would have a notable performance advantage over HashSet for storing/retrieving said elements. On paper, Array guarantees constant time insertion (since I know the size ahead of time) and retrieval, but the code for HashSet looks much cleaner and adds some flexibility, so

Make HashSet<string> case-insensitive

故事扮演 提交于 2019-11-29 09:02:25
I have method with HashSet parameter. And I need to do case-insensitive Contains within it: public void DoSomething(HashSet<string> set, string item) { var x = set.Contains(item); ... } Is it any way to make existing HashSet case-insensitive (do not create new one)? I'm looking for solution with best perfomance. Edit Contains can be called multiple times. So IEnumerable extensions are not acceptable for me due to lower perfomance than native HashSet Contains method. Solution Since, answer to my question is NO, it is impossible, I've created and used following method: public HashSet<string>

Changing the elements in a set changes the 'equals' semantics

て烟熏妆下的殇ゞ 提交于 2019-11-29 08:15:47
Imagine that we have this piece of code. public class HashAddAfter { private class A { public int value; public A(int value) { this.value = value; } public void setValue(int value) { this.value = value; } // Code for hashCode()... // Code for equals()... } Set<A> list1 = new HashSet<A>(); Set<A> list2 = new HashSet<A>(); public static void main(String[] args) { HashAddAfter x = new HashAddAfter(); A e1 = x.new A(1); A e2 = x.new A(1); x.list1.add(e1); x.list2.add(e2); System.out.println(x.list1.equals(x.list2)); // true e1.setValue(4); e2.setValue(4); System.out.println(x.list1.equals(x.list2)

C++ how to insert array into hash set?

做~自己de王妃 提交于 2019-11-29 06:16:16
I need to insert a 1D array into the hashset. But I got error while compiling. #include <stdio.h> #include <stdlib.h> #include <hash_set.h> using namespace std; int hash_comp(const int* state1,const int* state2) { int result = 0; for (i = 0; i < 16; i++) { if (state1[i] != state2[i]) { result = -1; } } return result; } struct eqArray { bool operator()(const int* a1,const int* a2) const { return hash_comp(a1,a2) == 0; } }; hash_set<int*,hash<int*>,eqArray> closelist; int main(int argc, char** argv) { const int sn[16] = {1,2,3,4,5,6,0,8,9,10,11,12,13,14,7,15}; closelist.insert(sn); return 0; }