1

I am new to Java 8 lambdas. I have a code starts with:

StringBuilder lemma = new StringBuilder("(");

and than two pieces of codes. First one:

lemma.append("(");

for (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("(");

for (String mt : mts) {
   lemma.append("label2:").append(mt).append(OPERATOR);
 }

lemma.delete(lemma.length() - OPERATOR.length(), lemma.length());
lemma.append(")");

How can I make a function which covers that 2 pieces of code which accepts arguments:

List<String> (which is for -dts- or -mts-)

and

String (which is for -"label1:"- or -"label2:"-)   

and

func() (which is for -labelList.getLabel1(dt)- or -mt-)

Is it possible to do such a thing with Java 8 lambdas?

5
  • 2
    Are you asking if you can differentiate the behavior of your String building logic based on the name of your Iterable variable reference? That would be a no. Commented Nov 17, 2016 at 17:04
  • You can pass the string, list, and a lambda to append as parameters. Commented Nov 17, 2016 at 17:05
  • I don't understand your question. Can you give an example of how you'd like to invoke these lambdas? Commented Nov 17, 2016 at 17:12
  • Also, while using Peruvian placenames is cute, it adds a lot of cognitive overload for non-Peruvian readers. Nor does it help much if you use "English" placenames: london.getBristol(mt) is nonsensical. Maybe update the question with some variable names that make sense in some domain. Commented Nov 17, 2016 at 17:16
  • I'm trying to find a solution for either passing a String or a function to a function. Commented Nov 18, 2016 at 10:57

2 Answers 2

1

You could write it like this

public static <T> String dump(List<T> list, String desc, Function<T, String> func) {
    StringBuilder lemma = new StringBuilder();
    String sep = "(";
    for (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 a = dump(dts, "cuzco:", huancayo::getCuzco);
 String b = dump(mts, "cucuta:", m -> m);
Sign up to request clarification or add additional context in comments.

2 Comments

Great! How about the performance effect of calling an apply method of a Function?
@kamaci it is pretty small compared to appending strings to a StringBuilder
0

Using Stream and StringJoiner / Collectors.joining (and aligning to Peters accepted answer):

public static <T> String dump(String label, List<T> list, Function<T, String> func) {
  return list.stream()
             .map(s -> label + func.apply(s))
             .collect(Collectors.joining(OPERATOR, "(", ")"));
}

The call would be the same:

String a = dump(dts, "label1:", labelList::getLabel1);
String b = dump(mts, "label2:", m -> m);
// or: dump(mts, "label2:", Function.identity())

4 Comments

I read something about using stream-map-collect kills the performance?
It depends (as always). I read something about streams not being that fast yet as we wish them to be in some cases, but are faster in some other cases. Depends on the operations carried out and if you go parallel or not (which does not always make sense)...all of that could change under the cover (JIT compiler?). Is this piece of code performance critical? Really, really performance critical? If so, you might want to omit it altogether? ;-) I would always go for readability if performance is not the topmost requirement. If it is I would design for it appropriately.
Yes, this is not a performance crucial code :) On the other hand sometimes its not readable to use lambdas :)
If it's not really readable then lambda was either not the appropriate solution or it was badly used.. :-) if you mean that streams are not that readable, you get used to it ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.