Java语言中的Set类
这是MySet类的完整实现。
示例
class MySet {
constructor() {
this.container = {};
}
display() {
console.log(this.container);
}
has(val) {
return this.container.hasOwnProperty(val);
}
add(val) {
if (!this.has(val)) {
this.container[val] = val;
return true;
}
return false;
}
delete(val) {
if (this.has(val)) {
delete this.container[val];
return true;
}
return false;
}
clear() {
this.container = {};
}
forEach(callback) {
for (let prop in this.container) {
callback(prop);
}
}
static union(s1, s2) {
if (!s1 instanceof MySet || !s2 instanceof MySet) {
console.log("The given objects are not of type MySet");
return null;
}
let newSet = new MySet();
s1.forEach(elem => newSet.add(elem));
s2.forEach(elem => newSet.add(elem));
return newSet;
}
static difference(s1, s2) {
if (!s1 instanceof MySet || !s2 instanceof MySet) {
console.log("The given objects are not of type MySet");
return null;
}
let newSet = new MySet();
s1.forEach(elem => newSet.add(elem));
s2.forEach(elem => newSet.delete(elem));
return newSet;
}
}热门推荐
5 初八祝福语简短语
8 合字的祝福语简短
10 送外卖祝福语大全简短
11 公司开年仪式祝福语简短
12 红颜生日祝福语简短独特
13 儿女的生日祝福语简短
14 祝福语写给自己的简短
15 送笔筒的祝福语简短
16 老师出国祝福语大全简短
17 六一祝福语简短感动女生
18 女生 节日祝福语简短大气