Skip to content

Instantly share code, notes, and snippets.

@bestK
Last active November 17, 2017 08:28
Show Gist options
  • Save bestK/393106c1aff7fdaf8078fc3b694ae82b to your computer and use it in GitHub Desktop.
Save bestK/393106c1aff7fdaf8078fc3b694ae82b to your computer and use it in GitHub Desktop.
Java syntax sugar 自实现的语法糖
public class MapFactory {
private HashMap<String, Object> map = new HashMap<>();
public HashMap<String, Object> getMap() {
return map;
}
public MapFactory setMap(HashMap<String, Object> map) {
this.map = map;
return this;
}
public MapFactory put(String k1, Object v1) {
map.put(k1, v1);
return this;
}
}
class test(){
@Test
public void test() {
Long begin = System.currentTimeMillis();
MapFactory mapFactory = new MapFactory();
mapFactory.put("msgtype", "markdown")
.put("markdown", new MapFactory().put("text", "1").put("title", "1").getMap())
.put("at", new MapFactory().put("atMobiles", new String[]{"1", "2"}).getMap())
.put("isAtAll", false);
Long end = System.currentTimeMillis();
System.out.println(JSON.toJSONString(mapFactory.getMap()));
System.out.println((end - begin) + "ms");
}
}
result:{"isAtAll":false,"at":{"atMobiles":["1","2"]},"markdown":{"text":"1","title":"1"},"msgtype":"markdown"}
0ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment