Consumer (Interface)

Consumer (I)

Consumer is a functional interface which contains only one method i.e. accept().

It accept some input and perform required operations and not required to return any value.

 Interface Consumer<T>

{

      public void accept(T t);

}


Function Chaining

import java.util.function.*;

class FunctionChaining {

  public static void main(String[] args) {
    Function<Integer, Integer> f1 = i -> i * 2;
    Function<Integer, Integer> f2 = i -> i * i * i;
    //first f1 then f2;
    System.out.println(f1.andThen(f2).apply(2));
    //first f2 then f1;
    System.out.println(f1.compose(f2).apply(2));
    System.out.println(f2.andThen(f1).apply(2));
  }
}

Function Chaining

 Function Chaining

Two functions can be combined to from a more complex function

first f1 then f2

i.e. f1.andThen(f2).apply(i);

first f2 then f1

f1.compose(f2).apply(i);


Getting the grade of Student using Function

 

import java.util.function.*;

class Student {

  String name;
  int marks;

  Student(String name, int marks) {
    this.name = name;
    this.marks = marks;
  }
}

class Result {

  public static void main(String[] args) {
    Function<Student, String> f = s -> {
      int marks = s.marks;
      String grade = " ";
      if (marks >= 80) grade = "A [Dictinction]"; else if (marks >= 60) grade =
        "B [First Division]"; else if (marks >= 50) grade =
        "B [Second Division]"; else if (marks >= 35) grade =
        "D [Third Division]"; else grade = "E [Fail]";
      return grade;
    };
    Student[] s = {
      new Student("Sudeep", 100),
      new Student("Vijay", 65),
      new Student("Rama", 55),
      new Student("Chinny", 45),
      new Student("Vinny", 35),
    };
    for (Student s1 : s) {
      System.out.println("Student name : " + s1.name);
      System.out.println("Student marks : " + s1.marks);
      System.out.println("Student grade : " + f.apply(s1));
      System.out.println("-----------------------------------");
    }
  }
}

Method and Constructor references by using :: (double colon) operator

 

Functional Interface method can be mapped to our specified method by using :: (double colon) operator. This is called method reference.

Our specified method can be either static method or instance method.

Functional Interface method and our specified method should have same argument types, except this the remaining things like return type, method name, modifiers etc. are not required to match.

 

Syntax:

Case1:

If our specified method is static method

classname::methodName

 

Case2:

If the method name is instance method

objRef::methodName

 

Functional Interface can refer lambda expression and Functional Interface can also refer method reference. Hence lambda expression can be replace with method reference.

Hence method reference is alternative syntax to lambda expression.


Method Reference


//With lambda Expression
class MethodRef {

  public static void main(String[] args) {
    Runnable r = () -> {
      for (int i = 0; i <= 10; i++) {
        System.out.println("Child Thread");
      }
    };
    Thread t = new Thread(r);
    t.start();
    for (int i = 0; i <= 10; i++) {
      System.out.println("Main Thread");
    }
  }
}

//With Method Reference

class MethodRef {

  public static void m1() {
    for (int i = 0; i <= 10; i++) {
      System.out.println("Child Thread");
    }
  }

  public static void main(String[] args) {
    Runnable r = MethodRef::m1;
    Thread t = new Thread(r);
    t.start();
    for (int i = 0; i <= 10; i++) {
      System.out.println("Main Thread");
    }
  }
}

Writing a function to find length of given input string.

 

import java.util.Scanner;
import java.util.function.Function;

class StringLengthFunction {

  public static void main(String[] args) {
    Function<String, Integer> f = s -> s.length();
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a String");
    String str = sc.nextLine();
    while (true) {
      if (str.equals("0")) {
        break;
      }
      System.out.println("The length of String is : " + f.apply(str));
      System.out.println("--------------------------------------------");
      System.out.println("Enter another String");
      System.out.println("Press 0 to exit");
      str = sc.nextLine();
      if (str.equals("0")) {
        break;
      }
    }
  }
}

writing predicate and possible joining combination of predicates

 

/
import java.util.function.*;

class PredicateJoin {

  // method to print the numbers
  public static void printNum(Predicate<Integer> p, int[] x) {
    for (int i : x) {
      if (p.test(i)) {
        System.out.println(i);
      }
    }
  }

  //main method
  public static void main(String[] args) {
    int[] x = { 0, 5, 10, 15, 20, 25, 30 };
    //first predicate
    Predicate<Integer> p1 = i -> i > 10;
    //second predicate
    Predicate<Integer> p2 = i -> i % 2 == 0;
    System.out.println("The NUmbers Greater Than 10 :");
    printNum(p1, x);

    System.out.println("The Even Numbers are :");
    printNum(p2, x);

    System.out.println("The Numbers not Greater Than 10 :");
    printNum(p1.negate(), x);

    System.out.println("The Nmbers Greater Than 10 and Even :");
    printNum(p1.and(p2), x);

    System.out.println("The Numbers Greater Than 10 OR Even :");
    printNum(p1.or(p2), x);
  }
}

Write a predicate to check the length of given string is greater than or not.

import java.util.Scanner;

import java.util.function.*;

public class CheckStringLength {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a String");
    String name = sc.nextLine();
    while (true) {
      Predicate<String> p = s -> (s.length() > 3);
      System.out.println(p.test(name));
      System.out.println("--------------------------------");
      System.out.println("Enter another String");
      System.out.println("Type exit to close the program");
      name = sc.nextLine();
      if (name.equals("exit")) {
        break;
      }
    }
  }
}

Write a predicate to check Whether the given integer is greater than 10 or not.

 

//Write a predicate to check Whether the given integer is
//greater than 10 or not.
import java.util.function.*;
import java.util.Scanner;

class CheckNo{
    public static void main(String[] args) {
        Predicate<Integer> p = i->(i>10);
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter an integer");
        int n = sc.nextInt();
        while(n!=0){
            System.out.println(p.test(n));
            System.out.println("Enter an integer");
             n = sc.nextInt();
        }
    }
}

Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...