packagecom.hdwang;importjava.util.HashMap;importjava.util.Map;/*** Created by hdwang on 2017/12/19.*/
public classMyTemplate {public static voidmain(String[] args){
String template= "${name},${sex},${birthYear}年出生,${graduateYear}年毕业于${university}。";
Map params = new HashMap<>();
params.put("name","张三");
params.put("sex","男");
params.put("birthYear","1990");
params.put("graduateYear","2012");
params.put("university","清华大学");long start =System.currentTimeMillis();for(int i=0;i<10000;i++) {
String result=render(template, params);if(i==9999) {
System.out.println(result);
}
}long end =System.currentTimeMillis();
System.out.println("cost time:"+(end-start)+"ms");
start=System.currentTimeMillis();for(int i=0;i<10000;i++) {
String result=render2(template, params);if(i==9999) {
System.out.println(result);
}
}
end=System.currentTimeMillis();
System.out.println("cost time:"+(end-start)+"ms");
}public static String render(String template,Mapparams){//使用builder拼接,比string相加提高不少效率
StringBuilder builder = newStringBuilder();//定义控制变量
boolean $Begin = false;boolean paramBegin = false;//boolean paramEnd = false;
StringBuilder key = null;//循环匹配
for(int i=0;i
if(c=='$'){
$Begin= true;
}if($Begin && c=='{'){
paramBegin= true;
builder.deleteCharAt(builder.length()-1); //删除添加的$字符
key= newStringBuilder();continue;
}//参数key
if(paramBegin && c!='}'){if(c=='{'){
System.out.println("模板格式错误!位置:"+i);
}else{
key.append(c);
}continue;
}//结束标识
if(paramBegin && c=='}'){//paramEnd = true;//拼接参数key对应的值
builder.append(params.get(key.toString()));//重置控制变量
$Begin = false;
paramBegin= false;//paramEnd = false;
continue;
}//默认情况
builder.append(c); //添加字符
}returnbuilder.toString();
}public static String render2(String template,Mapparams){for(Map.Entryentry:params.entrySet()){
String key=entry.getKey();
String value=entry.getValue();
template= template.replace("${"+key+"}",value);
}returntemplate;
}
}