JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换类

[java] view plain copy
 
  1. <pre name="code" class="java"></pre><pre name="code" class="java"><pre name="code" class="java">import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. public class Utils {  
  7.   
  8.     /** 
  9.      * 定义分割常量 (#在集合中的含义是每个元素的分割,|主要用于map类型的集合用于key与value中的分割) 
  10.      */  
  11.     private static final String SEP1 = "#";  
  12.     private static final String SEP2 = "|";  
  13.   
  14.     /** 
  15.      * List转换String 
  16.      *  
  17.      * @param list 
  18.      *            :需要转换的List 
  19.      * @return String转换后的字符串 
  20.      */  
  21.     public static String ListToString(List<?> list) {  
  22.         StringBuffer sb = new StringBuffer();  
  23.         if (list != null && list.size() > 0) {  
  24.             for (int i = 0; i < list.size(); i++) {  
  25.                 if (list.get(i) == null || list.get(i) == "") {  
  26.                     continue;  
  27.                 }  
  28.                 // 如果值是list类型则调用自己  
  29.                 if (list.get(i) instanceof List) {  
  30.                     sb.append(ListToString((List<?>) list.get(i)));  
  31.                     sb.append(SEP1);  
  32.                 } else if (list.get(i) instanceof Map) {  
  33.                     sb.append(MapToString((Map<?, ?>) list.get(i)));  
  34.                     sb.append(SEP1);  
  35.                 } else {  
  36.                     sb.append(list.get(i));  
  37.                     sb.append(SEP1);  
  38.                 }  
  39.             }  
  40.         }  
  41.         return "L" + EspUtils.EncodeBase64(sb.toString());  
  42.     }  
  43.   
  44.     /** 
  45.      * Map转换String 
  46.      *  
  47.      * @param map 
  48.      *            :需要转换的Map 
  49.      * @return String转换后的字符串 
  50.      */  
  51.     public static String MapToString(Map<?, ?> map) {  
  52.         StringBuffer sb = new StringBuffer();  
  53.         // 遍历map  
  54.         for (Object obj : map.keySet()) {  
  55.             if (obj == null) {  
  56.                 continue;  
  57.             }  
  58.             Object key = obj;  
  59.             Object value = map.get(key);  
  60.             if (value instanceof List<?>) {  
  61.                 sb.append(key.toString() + SEP1 + ListToString((List<?>) value));  
  62.                 sb.append(SEP2);  
  63.             } else if (value instanceof Map<?, ?>) {  
  64.                 sb.append(key.toString() + SEP1  
  65.                         + MapToString((Map<?, ?>) value));  
  66.                 sb.append(SEP2);  
  67.             } else {  
  68.                 sb.append(key.toString() + SEP1 + value.toString());  
  69.                 sb.append(SEP2);  
  70.             }  
  71.         }  
  72.         return "M" + EspUtils.EncodeBase64(sb.toString());  
  73.     }  
  74.   
  75.     /** 
  76.      * String转换Map 
  77.      *  
  78.      * @param mapText 
  79.      *            :需要转换的字符串 
  80.      * @param KeySeparator 
  81.      *            :字符串中的分隔符每一个key与value中的分割 
  82.      * @param ElementSeparator 
  83.      *            :字符串中每个元素的分割 
  84.      * @return Map<?,?> 
  85.      */  
  86.     public static Map<String, Object> StringToMap(String mapText) {  
  87.   
  88.         if (mapText == null || mapText.equals("")) {  
  89.             return null;  
  90.         }  
  91.         mapText = mapText.substring(1);  
  92.   
  93.         mapText = EspUtils.DecodeBase64(mapText);  
  94.   
  95.         Map<String, Object> map = new HashMap<String, Object>();  
  96.         String[] text = mapText.split("\" + SEP2); // 转换为数组  
  97.         for (String str : text) {  
  98.             String[] keyText = str.split(SEP1); // 转换key与value的数组  
  99.             if (keyText.length < 1) {  
  100.                 continue;  
  101.             }  
  102.             String key = keyText[0]; // key  
  103.             String value = keyText[1]; // value  
  104.             if (value.charAt(0) == 'M') {  
  105.                 Map<?, ?> map1 = StringToMap(value);  
  106.                 map.put(key, map1);  
  107.             } else if (value.charAt(0) == 'L') {  
  108.                 List<?> list = StringToList(value);  
  109.                 map.put(key, list);  
  110.             } else {  
  111.                 map.put(key, value);  
  112.             }  
  113.         }  
  114.         return map;  
  115.     }  
  116.   
  117.     /** 
  118.      * String转换List 
  119.      *  
  120.      * @param listText 
  121.      *            :需要转换的文本 
  122.      * @return List<?> 
  123.      */  
  124.     public static List<Object> StringToList(String listText) {  
  125.         if (listText == null || listText.equals("")) {  
  126.             return null;  
  127.         }  
  128.         listText = listText.substring(1);  
  129.   
  130.         listText = EspUtils.DecodeBase64(listText);  
  131.   
  132.         List<Object> list = new ArrayList<Object>();  
  133.         String[] text = listText.split(SEP1);  
  134.         for (String str : text) {  
  135.             if (str.charAt(0) == 'M') {  
  136.                 Map<?, ?> map = StringToMap(str);  
  137.                 list.add(map);  
  138.             } else if (str.charAt(0) == 'L') {  
  139.                 List<?> lists = StringToList(str);  
  140.                 list.add(lists);  
  141.             } else {  
  142.                 list.add(str);  
  143.             }  
  144.         }  
  145.         return list;  
  146.     }  
  147.   
  148. }  
  149. </pre><br>  
  150. <pre></pre>  
  151. 最终版本  
  152. <pre></pre>  
  153. <pre name="code" class="java">运行结果:<img src="http://hi.csdn.net/attachment/201109/29/0_1317259591S9xc.gif" alt=""></pre>  
  154. <pre></pre>  
  155.      
  156. </pre>