js创建javaMap

/** 
* Simple Map 
* var m = new Map(); 
* m.put('key','value'); 
* var v_otherMap = v_m.toMapString();*
* alert(v_otherMap ); 
*/
(function(){ var _common = { Map :function () { Array.prototype.remove = function(val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } }; this.keys = new Array(); /** 存放键的数组(遍历用到) */ this.data = new Object();/** 存放数据 */ /** * 放入一个键值对 * @param {String} key * @param {Object} value */ this.put = function(key, value) { if(this.data[key] == null){ this.keys.push(key); } this.data[key] = value; }; /** * 获取某键对应的值 * @param {String} key * @return {Object} value */ this.get = function(key) { return this.data[key]; }; /** * 删除一个键值对 * @param {String} key */ this.remove = function(k) { this.keys.remove(k); //delete this.keys[k]; this.data[k] = null; }; /** * 遍历Map,执行处理函数 * * @param {Function} 回调函数 function(key,value,index){..} */ this.each = function(fn){ if(typeof fn != 'function'){ return; } var len = this.keys.length; for(var i=0;i<len;i++){ var k = this.keys[i]; fn(k,this.data[k],i); } }; /** * 获取键值数组(类似Java的entrySet()) * @return 键值对象{key,value}的数组 */ this.entrys = function() { var len = this.keys.length; var entrys = new Array(len); for (var i = 0; i < len; i++) { entrys[i] = { key : this.keys[i], value : this.data[i] }; } return entrys; }; /** * 判断Map是否为空 */ this.isEmpty = function() { return this.keys.length == 0; }; /** * 获取键值对数量 */ this.size = function(){ return this.keys.length; }; /** * 重写toString */ this.toString = function(){ var s = "{"; for(var i=0;i<this.keys.length;i++,s+=','){ var k = this.keys[i]; s += k+"="+this.data[k]; } s+="}"; return s; }; /** * 重写toString */ this.toMapString = function(){ var s = "java.util.Map map = new java.util.HashMap();"; for(var i=0;i<this.keys.length;i++){ var k = this.keys[i]; if(i != 0) s +=";"; s += "map.put(\""+k+"\",\""+this.data[k]+"\")"; } s+="; return map;"; return s; }; } }; if(typeof JBPM == "undefined") window.JBPM={}; JBPM.common = _common; })();





    @SuppressWarnings("rawtypes")
    public static void main(String[] args) throws EvalError {
        StringBuffer sb = new StringBuffer();
        String tt ="201503";
        Map  map =    makeupDynamicMap("java.util.Map map = new java.util.HashMap();map.put(\"result\",\"to\");map.put(\"result1\",\"to111\"); return map;");
        System.out.println(map.get("result1"));
    }

    @SuppressWarnings("rawtypes")
    public static Map makeupDynamicMap(String otherParam) throws EvalError
    {
        Map variables = new HashMap();
        if(StringUtils.isEmpty(otherParam))
            return variables;
        if(StringUtils.isNotEmpty(otherParam))
        {
            Interpreter it = new Interpreter();
            Object obj = null;
            obj = it.eval(otherParam);
            variables.putAll((Map)obj);
        }
        return variables;
    }

 



 

 

 

posted @ 2015-09-09 16:38  也许还年轻  阅读(867)  评论(0编辑  收藏  举报