Java Higher Order Functions

In this tutorial, we will learn about the Higher Order Functions in java. Higher-Order Functions are the functions that either take a function as a parameter or return the function as output. We can say that the Higher-Order functions are the functions that operate on the other function.

We will take two examples of the Java Higher-Order Functions.

  1. sorting collections
  2. And sorting in reverse order

Sorting collections

Collections refer to the List, ArrayList, Maps, etc. And Sorting means arranging data either in ascending order or descending order. We can arrange data on the basis of the ASCII number in the case of Strings.

In the case of sorting collections, we will pass the function as a parameter.

Example of Sorting Collections::

package javaapplication29;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class NewClass5 {
    public static void main(String args[]){
    List<String> strlist= new ArrayList<>();
    strlist.add("kannu");
    strlist.add("anuj");
    strlist.add("Kamal");
    strlist.add("Anuj");
    Collections.sort(strlist, (String a, String b)->{
    return a.compareTo(b);
});
    System.out.println(strlist);

    
}
}

In the above code javaapplication29 is the name of the package. NewClass5 is the name of the class.

We are importing List and ArrayList from the util package as we are using list and ArrayList in the main method of our class. First of all, we created the list with object strlist. Then we added 4 entries to our list ie “kannu”, “anuj”, “Kamal”, “Anuj”. And now we want to arrange data. In this case, data will be arranged on the basis of the ASCII code.

We are using Collection.sort() method to arrange our data. In this method, we are passing an object of the List and Further String a and String b which is further used for comparison of the ASCII code.

String a and String b are the parameter of the function inside the function. (ie. function as a parameter of the function). This function is known as the lambda function. The lambda function is the function passed as a parameter to the function.

In this method, we are passing only two arguments first one is the object of our list and the second one is the lambda method.

Output

Higher Order Functions

Sorting in reverse order

In the case of sorting data in reverse order, we will get function as our output or we can say that In this case function will return another function. In this case, we will implement the comparator interface. And then we will create a Lambda function.

package javaapplication29;

import java.util.ArrayList;
import java.util.Collections;
import static java.util.Collections.list;
import java.util.Comparator;
import java.util.List;


public class NewClass6 {
    public static void main(String args[]){
    Comparator<String> cmprtr = (String a, String b) -> {
    return a.compareTo(b);
};
    List<String> strlist= new ArrayList<>();
    strlist.add("kannu");
    strlist.add("anuj");
    strlist.add("Kamal");
    strlist.add("Anuj");

Comparator<String> comprev = cmprtr.reversed();

Collections.sort(strlist, comprev);

System.out.println(strlist);
}
}

In the above code, javaapplication29 is the name of our package.NewClass6 is the name of our class. In the main method of our class, first of all, we created the lambda function. Then we created a list and Added the elements to our list.

We call the reversed method with the help of lambda expression which further calls another method which reverses the result returned by first comparator.

Just because the reversed method returns Lambda function( Function which is passed as a parameter in other functions), therefore, it is known as Higher-order Function.

Output::

Higher Order Functions

We can run the given codes using any IDE ie. Notepad++, Notepad, Eclipse, Netbeans. We do not require to add any external jar in case of the given programs so running them on any IDE will be an easy task.

Also read: How to plot Graph in java

Leave a Reply

Your email address will not be published. Required fields are marked *