Skip to content

Commit c135cdd

Browse files
HashMapExample.html.
1 parent 2744775 commit c135cdd

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

examples/HashMapExample.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>HashMap Example</title>
6+
</head>
7+
<body>
8+
Open the browser console to see the results.
9+
<script src="../src/hashmap.js"></script>
10+
<script>
11+
class Point{
12+
constructor(x, y){
13+
this.x = x;
14+
this.y = y;
15+
}
16+
hashCode(){
17+
const prime = 31;
18+
let result = 1;
19+
result = prime * result + this.x;
20+
result = prime * result + this.y;
21+
return result;
22+
}
23+
equals(obj){
24+
return obj instanceof Point && obj.x === this.x && obj.y === this.y;
25+
}
26+
}
27+
const hm = new HashMap();
28+
console.log('hm.put(new Point(1,2), "abc") ->', hm.put(new Point(1,2), "abc"));
29+
console.log('hm.containsKey(new Point(1,2)) ->', hm.containsKey(new Point(1,2)));
30+
console.log('hm.get(new Point(1,2)) ->', hm.get(new Point(1,2)));
31+
console.log('hm.put(new Point(1,2), "abcd") ->', hm.put(new Point(1,2), "abcd"));
32+
console.log('hm.get(new Point(1,2)) ->', hm.get(new Point(1,2)));
33+
</script>
34+
</body>
35+
</html>

0 commit comments

Comments
 (0)