How do I get structural equality while using the HashSet ? #190
Unanswered
janand1977
asked this question in
Q&A
Replies: 2 comments 1 reply
-
Try this: function isOrderedSetEqual(st1: OrderedSet<unkonwn>, st2: OrderedSet<unkonwn>) {
if (st1.size() !== st2.size()) {
return false;
}
return Array.from(st1).toString() === Array.from(st2).toString();
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
ah sorry i miss the question at the first time, i'm afraid the class Vehicle {
constructor(name, model, owner) {
this.name = name;
this.model = model;
this.owner = owner;
}
// your hash function here, please ensure it returns a unique value for whole database
toString() { return this.name + this.model + this.owner; }
}
const x = new Vehicle("Honda", "crv", "anand");
const y = new Vehicle("nissan", "versa", "sown");
const z = new Vehicle("Honda", "crv", "anand");
const mp = new HashMap();
mp.setElement(x.toString(), x);
mp.setElement(y.toString(), y);
// Then it works well
let it = mp.find(z.toString());
if (it.equals(mp.end())) {
mp.setElement(z.toString(), z);
} else {
console.log(it.pointer)
console.log('already found');
}
console.log(mp.length); // logs 2 because z is equals to x |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I need to find if a class object with same values (toString method) already exists in the collection. This does not work in the HashSet but works in the ordered set. Am I missing something?
Beta Was this translation helpful? Give feedback.
All reactions