上善若水
          In general the OO style is to use a lot of little objects with a lot of little methods that give us a lot of plug points for overriding and variation. To do is to be -Nietzsche, To bei is to do -Kant, Do be do be do -Sinatra
          posts - 146,comments - 147,trackbacks - 0
          <2011年9月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          常用鏈接

          留言簿(4)

          隨筆分類(157)

          隨筆檔案(125)

          收藏夾(13)

          Java GC

          Java General

          NoSQL

          Tech General

          Tech Master

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 895620
          • 排名 - 42

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

           1public class MapDeserialize {
           2    public static void main(String[] args) {
           3        Map<String, String> map = new HashMap<String, String>();
           4        map.put("key1""value1");
           5        map.put("key2"null);
           6        map.put("key3""");
           7        
           8        System.out.println(map);
           9        
          10        Map<String, String> emptyMap = new HashMap<String, String>();
          11        System.out.println(emptyMap);
          12        
          13        MapDeserialize deserialize = new MapDeserialize();
          14        String str1 = "{key3=, key2=null, key1=value1}";
          15        String str2 = "{}";
          16        Map<String, String> map1 = deserialize.str2Map(str1);
          17        System.out.println("map1: " + map1);
          18        Map<String, String> map2 = deserialize.str2Map(str2);
          19        System.out.println("map2: " + map2);
          20    }

          21    
          22    // We are assuming that the str is generated by map.toString(), so the str will be something like:
          23    // '{key3=, key2=null, key1=value1}' or '{}'
          24    public Map<String, String> str2Map(String str) {
          25        Map<String, String> map = new HashMap<String, String>();
          26        // The parameters map is empty
          27        if("{}".equals(str) || str == null || str.length() == 0{
          28            return map;
          29        }

          30        
          31        // Remove the '{' prefix and '}' suffix
          32        str = str.substring(1, str.length() - 1);
          33        String[] entries = str.split(",");
          34        for(String entry : entries) {
          35            String[] pair = entry.split("=");
          36            String key = pair[0].trim();
          37            if(pair.length == 1{
          38                map.put(key, "");
          39            }
           else {
          40                String value = pair[1].trim();
          41                if("null".equals(value)) {
          42                    map.put(key, null);
          43                }
           else {
          44                    map.put(key, value);
          45                }

          46            }

          47        }

          48        
          49        return map;
          50    }

          51}
          這段代碼貌似沒什么價(jià)值,只是保留著,以后再遇到相應(yīng)的情況,可以再做改進(jìn)。
          posted on 2011-09-20 15:54 DLevin 閱讀(524) 評(píng)論(0)  編輯  收藏 所屬分類: CodeTools
          主站蜘蛛池模板: 江西省| 阳东县| 霍城县| 通州市| 依安县| 卓尼县| 砀山县| 汉中市| 鸡东县| 沂源县| 德格县| 曲松县| 南和县| 中阳县| 赤峰市| 普兰店市| 德州市| 宣汉县| 库尔勒市| 沙河市| 车险| 涿鹿县| 清原| 博客| 平利县| 田东县| 沭阳县| 同心县| 光泽县| 石楼县| 贵定县| 平顺县| 新邵县| 灵石县| 鄄城县| 临湘市| 乌鲁木齐县| 得荣县| 阿尔山市| 霍邱县| 六安市|