JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换工具类(调优)

原文链接:http://blog.csdn.net/qq7342272/article/details/6830907#comments

调试时出现bug,在String中包含list嵌套或map嵌套时会出现字符串下标越界异常,原因是split分割时会出现""字符串,此时引用str.charAt(0)会抛出该异常,如图:

优化代码,经调试暂时解决String中2层嵌套list转换,如下:

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

测试代码如下:

 1 package test;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 public class StringUtilTest {
 9 
10     List<Object> list = new ArrayList<Object>();//外层list
11     List<Object> innerList = new ArrayList<Object>();//内层嵌套list
12     Map<String, Object> map = new HashMap<String, Object>();
13     String listText, mapText;
14 
15     /**
16      * 测试数据初始化
17      */
18     public StringUtilTest() {
19         innerList.add("innerlist1");
20         innerList.add("innerlist2");
21         list.add(innerList);
22         list.add("out1");
23         list.add("out2");
24 
25         map.put("innermap1", "v1");
26         map.put("innermap2", "v2");
27 
28         listText = "LLinnerlist1,innerlist2,,out1,out2,";//list字符串源
29 
30         mapText = "Minnermap2=v2|innermap1=v1|";//map字符串源
31     }
32 
33     public static void main(String[] args) {
34         StringUtilTest test = new StringUtilTest();
35 
36         String ls = test.testListToString();
37         System.out.println(ls);
38 
39         String ms = test.testMapToString();
40         System.out.println(ms);
41 
42         List<?> list = test.testStringToList();
43         System.out.println(list);
44 
45         Map<?, ?> map = test.testStringToMap();
46         System.out.println(map);
47     }
48 
49     public String testListToString() {
50         return CopyOfStringUtil.ListToString(list);
51     }
52 
53     public String testMapToString() {
54         return CopyOfStringUtil.MapToString(map);
55     }
56 
57     public List<?> testStringToList() {
58         return CopyOfStringUtil.StringToList(listText);
59     }
60 
61     public Map<?, ?> testStringToMap() {
62         return CopyOfStringUtil.StringToMap(mapText);
63     }
64 
65 }

测试结果,满足2层嵌套list:

posted @ 2017-04-09 12:50  賢偉  阅读(44408)  评论(1编辑  收藏  举报