站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Javascript 专题javascript函数库:HashSet.js

javascript函数库:HashSet.js

摘要: javascript函数库:HashSet.js
<textarea readonly style="border:none;font-family:Courier New;line-height:150%;width:760px;overflow-y:visible">

/**
* use as java.util.HashSet
*/

function testHashSet(){
alert("HashSet test begin:");
try{
}
catch(e){
alert(e);
}
alert("HashSet test end");
}

function HashSet(){

private:
this.map=new HashMap();
this.ZERO=new Integer(0);



function HashIterator(it){
this.it=it;

this.hasNext=hasNext;
function hasNext() {
return this.it.hasNext();
}

this.next=next;
function next() {
return this.it.next().getKey();
}
}

public:
this.size=size;
function size(){
return this.map.size();
}

this.isEmpty=isEmpty;
function isEmpty() {
return this.map.isEmpty();
}

this.contains=contains;
function contains(o) {
return this.map.containsKey(o);
}

this.add=add;
function add(o) {
return this.map.put(o,this.ZERO)==null;
}

this.addAll=addAll;
function addAll(set){
var mod=false;
for(var it=set.iterator();it.hasNext();){
if(this.add(it.next())) mod=true;
}
return mod;
}


this.remove=remove;
function remove(o) {
return this.map.remove(o).equals(this.ZERO);
}


this.clear=clear;
function clear() {
this.map.clear();
}


this.iterator=iterator;
function iterator(){
return new HashIterator(this.map.iterator());
}


this.equals=equals;
function equals(o) {
if (o.size() != this.size())
return false;
for(var it=this.iterator();it.hasNext();){
if(!o.contains(it.next())) return false;
}
return true;
}

this.hashCode=hashCode;
function hashCode() {
var h=0;
for(var it=this.iterator();it.hasNext();){
h+=it.next().hashCode();
}
return h;
}

this.toArray=toArray;
function toArray(){
var arr=new Array();
var i=0;
for(var it=this.iterator();it.hasNext();){
arr[i++]=it.next();
}
return arr;
}


}


</textarea>
↑返回目录
前一篇: javascript:HashMap.js
后一篇: 以一个最简单的例子把OO的JavaScript说明白