Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a limit of a maximum of 7 steps for "Function" "andThen()" "apply()" of Java?

I have the following code –

        Function<String, String> step1 = string -> string + " wakes up";
        Function<String, String> step2 = string -> string + "\nbrushes teeth";
        Function<String, String> step3 = string -> string + "\ngoes to toilet";
        Function<String, String> step4 = string -> string + "\ntakes a shower";
        Function<String, String> step5 = string -> string + "\nfeeds the cat";
        Function<String, String> step6 = string -> string + "\ncleans litter box";
        Function<String, String> step7 = string -> string + "\neats breakfast";
        Function<String, String> step8 = string -> string = "\ngoes for work";
        
        String name = "Neha";

System.out.println(step1.andThen(step2).andThen(step3).andThen(step4).andThen(step5).andThen(step6).andThen(step7).apply(name));

gives me output –

Neha wakes up
brushes teeth
goes to toilet
takes a shower
feeds the cat
cleans litter box
eats breakfast

But,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

System.out.println(step1.andThen(step2).andThen(step3).andThen(step4).andThen(step5).andThen(step6).andThen(step7).andThen(step8).apply(name));

Gives me output –

goes for work

So, I am wondering if there is a maximum limit of 7 steps here.

I am using Open JDK 11

>Solution :

No. Look at this:

Function<String, String> step8 = string -> string = "\ngoes for work";

Notice the assignment operator = instead of append +.

Change it to:

Function<String, String> step8 = string -> string + "\ngoes for work";

and it should show desired results.

Why would Java randomly limit it to 7 steps??

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading