Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Java 8 Higher-order functions

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 873
    Comment on it

    Higher-order means that now it is possible to pass the values as well as functions in the Methods same we can return both values and function from the methods.

    Example: public void show(String name, Function function){ }

    public Function createNewThing(Integer int)

    package com.evon;
    public class Time {
    
            public String toSeconds(int sec) {
                    return "time is in " + sec ;
            }
    
            public String toMinutes(int minute) {
                    return "time is in" + minute;
            }
    
            public String toHours(int hrs) {
                    return "time  is in " + hrs;
            }
    }
    

    Now the below code can show To pass function Inside the Methods:

    package com.evon;
    import java.util.function.Function;
    public class Print {
    
            public void print(int weight, Function<Integer, String> time) {
                    System.out.println("Print: " + scale.apply(time));
            }
    
            public static void main(String[] args) {
                    Print send = new Print();
                   Time time = new Time();
    
                    send.print(65, time::toSeconds);
    
                    send.print(65, time::toMinutes);
    
                    send.print(65, time::toHours);
    
            }
    }
    

    Function means that this function will accept an Integer and will return a String.

    time::toHours means that the method toHours of the time instance will be called.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: