Java8 New - Study Guide - Part 3
Java8 New - Study Guide - Part 3
Method References
Sometimes, You may have method already available with some logic which you want write
in the Lambda Expression. In this case, you can use existing method instead of duplicating
the code again.
You can use the Existing Method References to re-use the logic.
Method references are a special type of lambda expressions which are used to create
simple lambda expressions by referencing existing methods.
There are 3 types of method references.
1) Static Method Reference
2) Instance method Reference
3) Constructor Reference
A new operator ::(double colon) called as Method Reference Delimiter to specify the
Method References
Syntax:
Class::new
Ex:
Hello hello= Course::new;
1)Hello.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
public int test(int a, int b);
}
2)MyInteger.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class MyInteger {
public static int findSum(int a, int b) {
return a + b;
}
}
3)Demo1.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo1 {
System.out.println("Done!!!");
}
}
1)Hello.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
public int test(int a, int b);
}
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hai {
public void test(String str);
}
3)MyInteger.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
4)Demo2.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
}
}
1)Hello.java
package com.jlcindia.demo3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
public Course test(int a,String b,String c,String d);
}
public Course() {
System.out.println("Course - 0 arg Con");
}
public Course(int courseId, String courseName, String duration, String trainer) {
System.out.println("Course - 4 arg Con");
this.courseId = courseId;
this.courseName = courseName;
this.duration = duration;
this.trainer = trainer;
}
//Setters and Getters
//toString() method
}
3)Demo3.java
package com.jlcindia.demo3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo3 {
//1.Lambda Style
Hello hello1 = (a, b, c, d) -> {
Course course = new Course(a, b, c, d);
return course;
};
System.out.println("Done!!!");
}
}
1)Hello.java
package com.jlcindia.demo4;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
public void test(int[] arr);
}
2)Demo4.java
package com.jlcindia.demo4;
import java.util.Arrays;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
//1.Lambda Style
hello1.test(myarr1);
hello2.test(myarr2);
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
class Hello {
public static void show(int x) {
System.out.println(x);
}
}
public class Demo4 {
public static void main(String[] args) {
System.out.println("-------------------");
import java.util.ArrayList;
import java.util.List;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
class MyNumber {
public static boolean isEven(int number) {
if (number % 2 == 0)
return true;
else
return false;
}
public static boolean isOdd(int number) {
if (number % 2 != 0)
return true;
else
return false;
}
}
mylist.stream()
.filter(MyNumber::isEven)
.forEach(System.out::println);
System.out.println("-------------------");
mylist.stream()
.filter(MyNumber::isOdd)
.forEach(System.out::println);
System.out.println("-------------------");
mylist.stream()
.filter(a -> a % 2 == 0)
.forEach(a -> System.out.println(a));
System.out.println("-------------------");
Demo7.java
package com.jlcindia.demo7;
import java.util.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo6 {
public static void main(String[] args) {
mylist.stream()
.filter(a -> a % 2 == 0)
.map(a -> a* a)
.forEach(a -> System.out.println(a));
System.out.println("-------------------");
mylist.stream()
.filter(a -> a % 2 != 0)
.map(a -> a* a)
.forEach(a -> System.out.println(a));
}
}