1

I was just looking into the basics of functional programming. I want to convert the below code using lambda in Java. I am using java 8. any help will be appreciated.

Thanks.

        String reinBranches = (String) application.getAttribute("xx_xx_xx");

    if(reinBranches != null && reinBranches.length() > 0)
    {
        String reinBranchArray[] = reinBranches.split(",");
        for(int i = 0; i < reinBranchArray.length; i++)
        {
            if(reinBranchArray[i].equals((String) session.getAttribute("xyz_xyz_xyz"))) {
                return true;
            }
        }
    }
    return false;
5
  • The first rule to use a lambda is: is your target interface a functional interface? Commented Mar 20, 2018 at 12:02
  • 1
    You mean you want to convert the implementation to one with a stream? Commented Mar 20, 2018 at 12:03
  • @AniketSahrawat : The point is i am just a newbie on functional programming. I am in a phase of trying to convert my code. I just wanted to know if i can convert this small piece into lamda or not. Commented Mar 20, 2018 at 12:05
  • @daniu yes exactly! Commented Mar 20, 2018 at 12:22
  • why negative vote ?? Commented Mar 20, 2018 at 12:32

3 Answers 3

4

First I would get the attribute you want to match against and save it (before the lambda). Then stream the String[] from your split and return true if anyMatch your criteria. Finally, use a logical and to prevent NPE on the return. Like,

String reinBranches = (String) application.getAttribute("xx_xx_xx");
String xyz3 = (String) session.getAttribute("xyz_xyz_xyz");
return reinBranches != null && Arrays.stream(reinBranches.split(",")).anyMatch(xyz3::equals);

or as suggested in the comments using Pattern.splitAsStream which can short-circuit if a match is found without building the array from splitting

return reinBranches != null && Pattern.compile(",").splitAsStream(reinBranches).anyMatch(xyz3::equals);
Sign up to request clarification or add additional context in comments.

4 Comments

Pattern.compile(",").splitAsStream(reinBranches).anyMatch(s -> s.equals(session.getAttribute("xyz_xyz_xyz")));
There’s never a need for a boolean literal in a compound expression. In this specific case, the simplified expression is reinBranches != null && Arrays.stream(reinBranches.split(",")).anyMatch(attr::equals). Another alternative worth considering is to get rid of the split operation: reinBranches != null && reinBranches.matches( "(.*,)?"+Pattern.quote(xyz3)+"(,.*)?");
@Holger Very nice. I clearly need more coffee. Keurig on!
Pattern.splitAsStream has the advantage of not generating an array of all substrings in advance. Since anyMatch is short-circuiting, this can truly reduce the amount of work compared to String.split+Arrays.stream. On the other hand, String.split has an optimized code path for single character separators which likely wins for small to medium sized strings…
0

Magic

BooleanSupplier r = () -> {
    String reinBranches = (String) application.getAttribute("xx_xx_xx");

    if(reinBranches != null && reinBranches.length() > 0)
    {
        String reinBranchArray[] = reinBranches.split(",");
        for(int i = 0; i < reinBranchArray.length; i++)
        {
            if(reinBranchArray[i].equals((String) session.getAttribute("xyz_xyz_xyz"))) {
                return true;
            }
        }
    }
    return false;
}

1 Comment

@Holger also, it's community wiki so you could've just edited it
0

Just another way of doing same without any overhead of using streams:-

String reinBranches = (String) application.getAttribute("xx_xx_xx");
String xyz3 = (String) session.getAttribute("xyz_xyz_xyz");

return reinBranches != null && Pattern.compile(xyz3).matcher(reinBranches).find(0);

Comments

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.