用coffeescript实现类java的Map类
class Map
constructor : ->
@entry = {}
@count = 0 size : ->
return @count isEmpty : ->
return @count == 0 containsKey : (key) ->
if @isEmpty()
return false
return @entry.hasOwnProperty key containsValue : (val)->
if @isEmpty()
return false
for key,_val of @entry
if _val == val
return true
return false get : (key)->
if @isEmpty()
return null
if @containsKey key
return @entry[key]
return null put : (key, val)->
if !@entry.hasOwnProperty key
@count += 1;
@entry[key] = val
return @ remove : (key)->
if @isEmpty()
return false
if @containsKey key
delete @entry[key]
@count -= 1
return true
return false putAll : (map)->
if !map instanceof Map
return false
entry = map.entry
for key,val of entry
@put(key, val)
return true clear : ->
@entry = {}
@count = 0
return @ values : ->
vals = []
for key,val of @entry
vals.push val
return vals keySet : ->
keys = []
for key,val of @entry
keys.push key
return keys entrySet : ->
return @entry toString : ->
if typeof JSON == "undefined"
throw new Error "JSON object is not supported. Please check your browser version (IE8+, Firefox11+, Chrome19+, Safari5.1+)."
return JSON.stringify @entry valueOf : ->
return @toString()
使用coffeescript编译后生成代码:
(function() {
var Map;
var __hasProp = Object.prototype.hasOwnProperty;
Map = function() {
this.entry = {};
this.count = 0;
return this;
};
Map.prototype.size = function() {
return this.count;
};
Map.prototype.isEmpty = function() {
return this.count === 0;
};
Map.prototype.containsKey = function(key) {
if (this.isEmpty()) {
return false;
}
return this.entry.hasOwnProperty(key);
};
Map.prototype.containsValue = function(val) {
var _a, _val, key;
if (this.isEmpty()) {
return false;
}
_a = this.entry;
for (key in _a) {
if (!__hasProp.call(_a, key)) continue;
_val = _a[key];
if (_val === val) {
return true;
}
}
return false;
};
Map.prototype.get = function(key) {
if (this.isEmpty()) {
return null;
}
if (this.containsKey(key)) {
return this.entry[key];
}
return null;
};
Map.prototype.put = function(key, val) {
if (!this.entry.hasOwnProperty(key)) {
this.count += 1;
};
this.entry[key] = val;
return this;
};
Map.prototype.remove = function(key) {
if (this.isEmpty()) {
return false;
}
if (this.containsKey(key)) {
delete this.entry[key];
this.count -= 1;
return true;
}
return false;
};
Map.prototype.putAll = function(map) {
var _a, entry, key, val;
if (!map instanceof Map) {
return false;
}
entry = map.entry;
_a = entry;
for (key in _a) {
if (!__hasProp.call(_a, key)) continue;
val = _a[key];
this.put(key, val);
}
return true;
};
Map.prototype.clear = function() {
this.entry = {};
this.count = 0;
return this;
};
Map.prototype.values = function() {
var _a, key, val, vals;
vals = [];
_a = this.entry;
for (key in _a) {
if (!__hasProp.call(_a, key)) continue;
val = _a[key];
vals.push(val);
}
return vals;
};
Map.prototype.keySet = function() {
var _a, key, keys, val;
keys = [];
_a = this.entry;
for (key in _a) {
if (!__hasProp.call(_a, key)) continue;
val = _a[key];
keys.push(key);
}
return keys;
};
Map.prototype.entrySet = function() {
return this.entry;
};
Map.prototype.toString = function() {
if (typeof JSON === "undefined") {
throw new Error("JSON object is not supported. Please check your browser version (IE8+, Firefox11+, Chrome19+, Safari5.1+).");
}
return JSON.stringify(this.entry);
};
Map.prototype.valueOf = function() {
return this.toString();
};
})();
用coffeescript实现类java的Map类的更多相关文章
- 探究Java中Map类
Map以按键/数值对的形式存储数据,和数组非常相似,在数组中存在的索引,它们本身也是对象. Map的接口 Map---实现Map Map.Entry--Map的内部 ...
- [转] JAVA的Random类
Random类 (java.util) Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基础上进行一定的变换,从而产生需要 ...
- JAVA基础--常用类 String,StringBuffer, 基础数据类型包装类, Math类, Enum类
字符串相关类: String, StringBuffer String类为不可变的字符序列 String s1="hello"; String s2="hello&quo ...
- Java之File类
一.初见File类 java.io.File类代表系统中的文件(文件或目录) 常用构造方法 File(String pathname) File(String parent, String child ...
- Java Arrays工具类的使用
Arrays 类 java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的.具有以下功能: 给数组赋值:通过fill方法. 对数组排序:通过sort方法,按升序. 比较数组:通过 ...
- 关于JAVA的Random类的冷知识(转自菜鸟V)
JAVA的Random类(转) Random类 (java.util) Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基 ...
- Java.util.Math类--数学相关的工具类
Math类--数学相关的工具类 java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作. public static double abs(double ...
- java:常用类(包装类,equals和==的比较,Date,java.lang.String中常用方法,枚举enum)
*包装类: 将基本类型封装成类,其中包含属性和方法以方便对象操作. *byte---->Byte *short--->Short *long--->Long *float---> ...
- Java日期处理类的相关使用
java常用类-java日期处理类 Date类 Date类是jdk给我们提高的标准日期类,在java.util包下: 示例代码: import java.util.Date; public class ...
随机推荐
- 上传代码到github上
初始化 git init 添加远程仓库 git remote add origin[仓库名] 仓库地址 添加文件 git add . 本地提交 git commit -m 'message' 拉去远程 ...
- 面试===Linux试题及答案
一. 单选题: 1.添加一条静态路由,使到网络196.199.3通过eth2接口出去,用: A. route add -net 196.199.3.0 B. route add -net 196.19 ...
- Oracle基础 12 对象 objects 同义词/序列/试图/索引
--创建同义词create public synonym employees for hr.employees; --公共同义词需要 create public synonym 权限 表的所有用户授 ...
- 【snmp】华为和H3C 网络设备设置snmp
snmp-agent sys-info version all snmp community read public snmp community write private snmp sys-inf ...
- Selenium2+python自动化75-非input文件上传(SendKeys)【转载】
转至博客:上海-悠悠 前言 不少小伙伴问非input标签如何上传文档,这个本身就是一坑,无奈很多小伙伴非要跳坑里去,那就介绍一个非主流的上传文件方法吧,用第三方库SendKeys. (本篇基于pyth ...
- 详解nginx、php-fpm和mysql用户权限
通常情况下,我们运行web应用的服务器有CentOS.Ubuntu.Debian等等的Linux发行版本.这时候,构成服务架构所必须的Nginx.php和MySQL等应用的权限控制就显得非常重要,各个 ...
- Parsing Netflow using Kibana via Logstash to ElasticSearch
https://www.rsreese.com/parsing-netflow-using-kibana-via-logstash-to-elasticsearch/
- typescript 定义全局变量以及扩展原生js对象
使用“declare global”操作即可. 项目根目录下新建myDeclareFile.d.ts declare global { interface Navigator { mediaSessi ...
- HDU 1029 Ignatius and the Princess IV(数论)
#include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...
- Codeforces 189A. Cut Ribbon
题目链接:http://codeforces.com/problemset/problem/189/A 题意: 给你一个长度为 N 的布条, 再给你三个长度 a, b , c.你可以用剪刀去剪这些布条 ...