当前位置: 代码迷 >> J2SE >> 遍历List的算法
  详细解决方案

遍历List的算法

热度:230   发布时间:2016-04-24 00:20:42.0
求一个遍历List的算法
一个字符型setList,内容如下:
[2011-11, 2012-02, 2012-03, 2012-04, 2012-05, 2012-06, 2012-07]
需要转成json格式的结果
[{'year':2012,'month':['07','06','05','04','03','02']},{'year':2011,'month':['11']}
我现在用的嵌套for循环的方法,感觉代码很繁琐。
求更好的方法。

StringBuffer jsonStrBuf = new StringBuffer("[");
for (int i = setList.size() - 1; i > 0; i--) {
String setStr = setList.get(i);
String year = setStr.substring(0, 4);
String month = setStr.substring(5, setStr.length());
StringBuffer strBuf = new StringBuffer();
strBuf.append("{'year':" + year + ",'month':['" + month + "','");
for (int j = i - 1; j >= 0; j--) {
String setStrTemp = setList.get(j);
String yearTemp = setStrTemp.substring(0, 4);
String monthTemp = setStrTemp.substring(5, setStrTemp.length());
if (year.equals(yearTemp)) {
strBuf.append(monthTemp + "','");
i--;
} else {
if(i==1){
jsonStrBuf.append(strBuf.substring(0, strBuf.length() - 2) + "]},");
setStr = setList.get(0);
year = setStr.substring(0, 4);
month = setStr.substring(5, setStr.length());
strBuf = new StringBuffer();
strBuf.append("{'year':" + year + ",'month':['" + month + "','");
}else{
break;
}
}
}
jsonStrBuf.append(strBuf.substring(0, strBuf.length() - 2) + "]},");
}

------解决方案--------------------
不知道算不算简洁了些。。。

Java code
        StringBuffer jsonStrBuf = new StringBuffer("[");        String[] arr = { "2011-11", "2012-02", "2012-03", "2012-04", "2012-05", "2012-06", "2012-07" };        List<String> list = Arrays.asList(arr);        Map<String, List<String>> map = new HashMap<String, List<String>>();        for (String str : list) {            String y = str.substring(0, 4);            String m = str.substring(5);            if (map.containsKey(y)) {                map.get(y).add(m);            } else {                List<String> l = new ArrayList<String>();                l.add(m);                map.put(y, l);            }        }        for (String key : map.keySet()) {            jsonStrBuf.append("{'year':" + key);            jsonStrBuf.append(",'month':" + Arrays.toString((map.get(key).toArray(new String[0]))) + "},");        }        System.out.println(jsonStrBuf.delete(jsonStrBuf.length() - 1, jsonStrBuf.length()).append("]"));
------解决方案--------------------
for example
Java code
list = [2011-11, 2012-02, 2012-03, 2012-04, 2012-05, 2012-06, 2012-07];Map<String, List<String>> map = new HashMap<String, List<String>>();for (String s : list) { //把list解析,结果存到map里    String[] sa = s.split("-");    if (!map.containsKey(sa[0])) map.put(sa[0], new ArrayList<String>());    map.get(sa[0]).add(sa[1]);}StringBuilder buf = new StringBuilder();for (Map.Entry<String, List<String>> e : map.entrySet()) {    buf.append("{'year:").append(e.getKey()).append(",'month':[");    for (String s : e.getValue()) {        buf.append("'").append(s).append("',");    }    buf.delete(buf.length()-1, buf.length());    buf.append("},");}buf.delete(buf.length()-1, buf.length());System.out.println(buf);
  相关解决方案