爪哇8(411)
String m(String...m){LinkedHashMap<String,String>n=new LinkedHashMap<>();n.put("/","( * #, -");n.put("#","farm");n.put("-","E-I-E-I-O");n.put("+","here");n.put("*","had a");n.put("(","Old MacDonald");n.put("|"," a )");n.put(")","moo");n.put("moo",m[1]);n.put("cow",m[0]);m[0]="/,\nAnd on that # he * cow, -,\nWith|) + and|) t+,\nHere|, t+|, everyw+|),\n/!";n.forEach((k,v)->m[0]=m[0].replace(k,v));return m[0];}
滥用lambda,将替换放置在LinkedhashMap中,以使em保持定义的顺序,然后使用foreach lambda将键替换为主String中的值。参数被添加为地图中的最后2个替换项。varargs参数是要删除方法标题中的某些字节
非高尔夫版本:
String m(String... m)
{
LinkedHashMap<String, String> n = new LinkedHashMap<>();
n.put("/", "( * #, -");
n.put("#", "farm");
n.put("-", "E-I-E-I-O");
n.put("+", "here");
n.put("*", "had a");
n.put("(", "Old MacDonald");
n.put("|", " a )");
n.put(")", "moo");
n.put("moo", m[1]);
n.put("cow", m[0]);
m[0] = "/,\nAnd on that # he * cow, -,\nWith|) + and|) t+,\nHere|, t+|, everyw+|),\n/!";
n.forEach((k, v) -> m[0] = m[0].replace(k, v));
return m[0];
}