You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Alex Herbert (Jira)" <ji...@apache.org> on 2020/08/10 11:59:00 UTC

[jira] [Commented] (LANG-1594) Add support for lambda expressions in StringUtils.join

    [ https://issues.apache.org/jira/browse/LANG-1594?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17174267#comment-17174267 ] 

Alex Herbert commented on LANG-1594:
------------------------------------

You can already use lambdas with the streams API:
{code:java}
String ids = productList.stream()
                        .map(p -> String.valueOf(p.id))
                        .collect(Collectors.joining(","));
{code}
Or:
{code:java}
String prefix = "UPDATE product SET published=1 WHERE id IN (";
String suffix = ")";
String sql = productList.stream()
                        .map(p -> String.valueOf(p.id))
                        .collect(Collectors.joining(",", prefix, suffix));
{code}

A simple implementation that avoids the streams API is likely to be more efficient due to less object creation, but you then lose the flexibility of the streams API to do more than this simple example.


> Add support for lambda expressions in StringUtils.join
> ------------------------------------------------------
>
>                 Key: LANG-1594
>                 URL: https://issues.apache.org/jira/browse/LANG-1594
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.*
>    Affects Versions: 3.11
>            Reporter: Kiruahxh
>            Priority: Minor
>
> It would be a nice addition to support lambda expressions in the StringUtils.join overloads, they are supported in java 8  so there should not be compatibility problems.
> This would simplify this kind of code :
> {code:java}
> List<ProductOutput> productList = new ArrayList<Product>();
> ...
> String ids = "";
> int i = 0;
> for (Product p : productList) {
>     if (i != 0) {
>         ids += ",";
>     }
>     ids += p.id;
>     i++;
> }
> String sql = "UPDATE product SET published=1 WHERE id IN (" + list + ")";
> execUpdate(sql);{code}
> Fixed version :
> {code:java}
> List<Product> productList = new ArrayList<Product>();
> ...
> String ids = StringUtils.join(productList, ',', p -> String.valueOf(p.id))
> String sql = "UPDATE product SET published=1 WHERE id IN (" + list + ")";
> execUpdate(sql);
>  {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)