日期:2014-05-17 浏览次数:20660 次
import java.text.DecimalFormat; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Test { public static void main(String[] args) { String str = "9月:0@@@9月:9.10@@@10月:10.20@@@9月:9.20@@@10月:10.09@@@11月:11.35"; String p1 = "@@@"; String p2 = ":"; String[] strings = str.split(p1); Map<String, String> map = new HashMap<String, String>(); String[] tmpStrings ; for (String string : strings) { tmpStrings = string.split(p2); if (map.containsKey(tmpStrings[0])) { //月份后面的数字相加 float f = Float.valueOf(map.get(tmpStrings[0])) + Float.valueOf(tmpStrings[1]); map.put(tmpStrings[0], new DecimalFormat("#.00").format(f)); } else { map.put(tmpStrings[0], tmpStrings[1]); } } StringBuilder sBuilder = new StringBuilder(); Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next(); sBuilder.append(entry.getKey() + p2 + entry.getValue() + p1); } System.out.println(sBuilder.toString()); } }