Java 8 Lambdas Pass Function or Variable as a Parameter -
i new java 8 lambdas. have code starts with:
stringbuilder lemma = new stringbuilder("(");
and 2 pieces of codes. first one:
lemma.append("("); (string dt : dts) { lemma.append("label1:").append(labellist.getlabel1(dt)).append(operator); } lemma.delete(lemma.length() - operator.length(), lemma.length()); lemma.append(")");
second one:
lemma.append("("); (string mt : mts) { lemma.append("label2:").append(mt).append(operator); } lemma.delete(lemma.length() - operator.length(), lemma.length()); lemma.append(")");
how can make function covers 2 pieces of code accepts arguments:
list<string> (which -dts- or -mts-)
and
string (which -"label1:"- or -"label2:"-)
and
func() (which -labellist.getlabel1(dt)- or -mt-)
is possible such thing java 8 lambdas?
you write this
public static <t> string dump(list<t> list, string desc, function<t, string> func) { stringbuilder lemma = new stringbuilder(); string sep = "("; (t t : list) { lemma.append(sep).append(desc).append(func.apply(t)); sep = operator; } return lemma.length() == 0 ? "()" : lemma.append(")").tostring(); }
you can call these via
string = dump(dts, "cuzco:", huancayo::getcuzco); string b = dump(mts, "cucuta:", m -> m);
Comments
Post a Comment