Join using multiple arguments

40 views
Skip to first unread message

dschulten

unread,
Aug 14, 2012, 7:25:37 AM8/14/12
to lam...@googlegroups.com
Hi,

I need to join a list of objects and extract two properties of each list item, like this:

StringBuilder sb = new StringBuilder();
for(Item item : items) {

  if (sb.length() > 0)
    sb.append(", ");

  sb.append(item.getFoo());
  sb.append(" ");
  sb.append(item.getBar());
}

The result would be something like "foo1 bar1, foo2 bar2 ...". So I would like to join using multiple on() arguments and separatory in between each argument. Is that possible with Lambdaj at all? How would you go about this?

Best regards,
Dietrich

Mario Fusco

unread,
Aug 20, 2012, 4:02:15 AM8/20/12
to lam...@googlegroups.com
Hi Dietrich,

sorry for the delay in my reply, I was on vacation.
At the moment there isn't a lambdaj feature that could allow you to easily achieve this result unless you don't want to add a getFooBar() method to your bean like:

public String getFooBar() {
    return getFoo() + " " + getBar();
}

I guess this solution is the quickest thing you could do at the moment but I also have to admit that is impractical in many cases.
I guess I could extend the functionality of the join feature, by allowing the join method to accept a varargs of arguments.
I'll keep you updated if I'll decide to do that.

Mario

Dietrich Schulten

unread,
Aug 23, 2012, 11:07:13 AM8/23/12
to lam...@googlegroups.com

Hi Mario, thank you for the response. At least I know now it isn't possible.
Best regards
Dietrich

Reply all
Reply to author
Forward
0 new messages