JAVA8FEATURES
JAVA8FEATURES
—------------------------
Up to JAVA7 version, JAVA is an Object oriented Programming Language,
by understanding the advantages functional programming languages
features JAVA people want to introduce functional programming features
in JAVA, JAVA 8 version has the number of functional programming
features.
EX:
interface I{
void m1();
void m2();
static void m3(){
System.out.println("m3-I");
}
static void m4(){
System.out.println("m4-I");
}
}
class A implements I{
public void m1(){
System.out.println("m1-A");
}
public void m2(){
System.out.println("m2-A");
}
}
public class Main {
public static void main(String[] args) {
I i = new A();
i.m1();
i.m2();
A a = new A();
a.m1();
a.m2();
m1-A
m2-A
m1-A
m2-A
m3-I
m4-I
EX:
interface I{
static void m1(){
System.out.println("m1-I");
}
static void m2(){
System.out.println("m2-I");
}
static void m3(){
System.out.println("m3-I");
}
static void m4(){
System.out.println("m4-I");
}
}
m1-I
m2-I
m3-I
m4-I
EX:
interface Driver{
default void getDriverClass(){
System.out.println("sun.jdbc.odbc.JdbcOdbcDriver");
}
default void getDriverURL(){
System.out.println("jdbc:odbc:nag");
}
}
class OracleDriver implements Driver{
public void getDriverClass(){
System.out.println("oracle.jdbc.OracleDriver");
}
public void getDriverURL(){
System.out.println("jdbc:oracle:thin:@localhost:1521:xe")
;
}
}
class MySQLDriver implements Driver{
public void getDriverClass(){
System.out.println("com.mysql.cj.jdbc.Driver");
}
public void getDriverURL(){
System.out.println("jdbc:mysql://localhost:3300/
durgadb");
}
}
class MSAccessDriver implements Driver{
}
public class Main {
public static void main(String[] args) {
Driver oracleDriver = new OracleDriver();
oracleDriver.getDriverClass();
oracleDriver.getDriverURL();
System.out.println();
oracle.jdbc.OracleDriver
jdbc:oracle:thin:@localhost:1521:xe
com.mysql.cj.jdbc.Driver
jdbc:mysql://localhost:3300/durgadb
sun.jdbc.odbc.JdbcOdbcDriver
jdbc:odbc:nag
EX:
interface I1{
default void m1(){
System.out.println("m1-I1");
}
}
interface I2{
default void m2(){
System.out.println("m2-I2");
}
}
interface I3 extends I1, I2{
default void m3(){
System.out.println("m3-I3");
m1();
m2();
}
}
class A implements I3{
}
public class Main {
public static void main(String[] args) {
I3 i3 = new A();
i3.m3();
}
}
m3-I3
m1-I1
m2-I2
EX:
interface I1{
default void m1(){
System.out.println("m1-I1");
}
}
interface I2{
default void m1(){
System.out.println("m1-I2");
}
}
interface I3 extends I1, I2{
default void m3(){
System.out.println("m3-I3");
m1();
}
}
class A implements I3{
}
public class Main {
public static void main(String[] args) {
I3 i3 = new A();
i3.m3();
}
}
Functional Interfaces:
—------------------------
In Java , if we declare an interface without any abstract method then
that interface is called a marker interface, in Java applications, if
we declare any interface with exactly one abstract method, not more
than one and not less than one then that interface is called
Functional Interface.
EX:
1. Runnable: run() method
2. ActionListener: actionPerformed()
3. Comparable: compareTo()
—-----
—------
EX:
@FunctionalInterface
interface I{
void m1();
//void m2(); ---> Error
}
class A implements I{
public void m1(){
System.out.println("m1-A");
}
public void m2(){
System.out.println("m2-A");
}
}
public class Main {
public static void main(String[] args) {
I i = new A();
i.m1();
}
}
EX:
@FunctionalInterface
interface I1{
void m1();
}
interface I2 extends I1{
EX:
@FunctionalInterface
interface I1{
void m1();
}
interface I2 extends I1{
void m2();
}
EX:
@FunctionalInterface
interface I1{
void m1();
}
@FunctionalInterface
interface I2 extends I1{
EX:
@FunctionalInterface
interface I1{
void m1();
}
@FunctionalInterface
interface I2 extends I1{
void m2();
}
EX:
@FunctionalInterface
interface I1{
void m1();
}
@FunctionalInterface
interface I2 extends I1{
void m1();
}
Status: Valid
In Java applications, it is possible to provide a number of default
methods and static methods inside the Functional Interfaces along with
a single abstract method.
@FunctionalInterface
interface I1{
void m1();
default void m2(){
System.out.println("m2-I");
}
static void m3(){
System.out.println("m3-I");
}
}
Status: Valid
Lambda Expressions:
—--------------------
Lambda Expression is an anonymous Function, it does not have name,
return type , access modifiers, where the method declaration and
method body must be separated with -> symbol.
Syntax:
FunctionalInterface refVar = (Params) -> { —- };
refVar.methodName(paramValues);
EX:
Consider the Following Program before Lambda Expressions:
interface Wish{
public String sayHello(String name);
}
class WishImpl implements Wish{
public String sayHello(String name){
String message = "Hello "+name+", Good Morninig!";
return message;
}
}
public class Main {
public static void main(String[] args) {
Wish wish = new WishImpl();
System.out.println(wish.sayHello("Durga"));
}
}
The above program with Lambda Expression:
interface Wish{
public String sayHello(String name);
}
interface Wish{
public String sayHello(String name);
}
interface Wish{
public String sayHello(String name);
}
EX:
Before Lambda Expressions:
interface Welcome{
public void sayWelcome();
}
class WelcomeImpl implements Welcome{
public void sayWelcome(){
System.out.println("Welcome To Durgasoft!");
}
}
public class Main {
public static void main(String[] args) {
Welcome welcome = new WelcomeImpl();
welcome.sayWelcome();
}
}
EX:
interface Math{
String evenOrOdd(int num);
}
public class Main {
public static void main(String[] args) {
Math math = num -> num%2==0? num+" Is Even Number": num+"
is ODD Number";
System.out.println(math.evenOrOdd(10));
System.out.println(math.evenOrOdd(5));
}
}
EX:
interface Math{
int biggest(int num1, int num2);
}
public class Main {
public static void main(String[] args) {
Math math = (num1, num2) -> num1-num2>=0?num1:num2;
System.out.println("Biggest : "+math.biggest(10,20));
System.out.println("Biggest : "+math.biggest(20,10));
}
}
b2 = new Button("GREEN");
b2.setBackground(Color.green);
b2.setFont(font);
b2.addActionListener(myActionListener);
b3 = new Button("BLUE");
b3.setBackground(Color.blue);
b3.setFont(font);
b3.addActionListener(myActionListener);
this.add(b1);
this.add(b2);
this.add(b3);
}
public class Main {
public static void main(String[] args) {
ColorsFrame colorsFrame = new ColorsFrame();
}
}
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
b2 = new Button("GREEN");
b2.setBackground(Color.green);
b2.setFont(font);
b2.addActionListener(e ->
this.setBackground(Color.green));
b3 = new Button("BLUE");
b3.setBackground(Color.blue);
b3.setFont(font);
b3.addActionListener(e ->
this.setBackground(Color.blue));
this.add(b1);
this.add(b2);
this.add(b3);
}
}
public class Main {
public static void main(String[] args) {
ColorsFrame colorsFrame = new ColorsFrame();
}
}
}
}
}
}
@Override
public int compare(String str1, String str2) {
}
}
Method References:
—-----------------
In general, in Java applications, if we declare a function interface
then we are able to provide implementation for the functional
interface method either by using implementation class or by using
anonymous inner class or by using Lambda Expressions, where Lambda
Expressions are suggestible.
Syntax:
a) If the method is non static method
FunctionalInterface refVar = ClassRefVar::methodName;
b) If the method is a Static method:
FunctionalInterface refVar = ClassName::methodName;
interface Welcome{
void wish();
}
class Hello{
public void sayHello(){
System.out.println("Hello User, Welcome To Durgasoft....");
}
}
class Hi{
public static void sayHi(){
System.out.println("Hi User, Welcome to Durgasoft.....");
}
}
public class Main {
public static void main(String[] args) {
Hello hello = new Hello();
Welcome welcome1 = hello :: sayHello;
welcome1.wish();
Constructor Reference:
—---------------------
IN general, in Java applications, for the functional interface we are
able to provide implementation for the abstract method by providing an
implementation class or by using anonymous inner class or by using a
lambda expression.
For the Functional interface method , if we want to reference a
particular class constructor from the Functional interface method then
we have to use Constructor reference.
Syntax:
FunctionalInterface refVar = ClassName::new;
interface Welcome{
void wish();
}
class Hello{
public Hello(){
System.out.println("Hello class Constructor.....");
}
}
public class Main {
public static void main(String[] args) {
Welcome welcome = Hello :: new;
welcome.wish();
}
}
EX:
interface Welcome{
void wish(String name);
}
class Hello{
public Hello(){
}
public Hello(String name){
System.out.println("Hello class Constructor with the Parameter
Value "+name);
}
public void wishHello(String name){
System.out.println("Hello "+name+", Welcome to Durgasoft.....");
}
public static void wishHi(String name){
System.out.println("Hi "+name+", Welcome to Durgasoft......");
}
}
public class Main {
public static void main(String[] args) {
Welcome welcome1 = Hello :: new;
welcome1.wish("Durga");
1. Predicate
2. Function
3. Consumer
4. Supplier
Predicate:
—----------
Consider the below program
interface Math{
public boolean test(int num);
}
public class Main {
public static void main(String[] args) {
Math math = num -> num%2==0;
System.out.println("10 is Even Number? : "+math.test(10));
System.out.println("5 is Even Number? : "+math.test(5));
}
}
import java.util.function.Predicate;
/*
interface Predicate<T>{
public boolean test(T t);
}
*/
public class Main {
public static void main(String[] args) {
Predicate<Integer> predicate = num -> num%2==0;
System.out.println("10 is Even Number? : "+predicate.test(10));
System.out.println("5 is Even Number? : "+predicate.test(5));
}
}
EX:
import java.util.function.Predicate;
Predicate<Integer> predicate3 =
predicate1.and(predicate2);
System.out.println(predicate3.test(20));
System.out.println(predicate3.test(15));
System.out.println();
Predicate<Integer> predicate4 =
predicate1.or(predicate2);
System.out.println(predicate4.test(20));
System.out.println(predicate4.test(15));
System.out.println(predicate4.test(75));
System.out.println();
Predicate<Integer> predicate5 =
predicate1.negate();
System.out.println(predicate5.test(25));
System.out.println(predicate5.test(70));
}
}
Function:
—----------
Consider the following program
interface Math{
public int sqrRoot(int num);
}
public class Main {
public static void main(String[] args) {
Math math = num -> (int) java.lang.Math.sqrt(num);
System.out.println("sqrt of 16 : "+math.sqrRoot(16));
System.out.println("sqrt of 9 : "+math.sqrRoot(9));
}
}
EX:
import java.util.function.Function;
/*
public interface Function<T,R>{
public R apply(T t);
}
*/
public class Main {
public static void main(String[] args) {
Function<String, String> function = str ->
str.concat("@durgasoft.com");
System.out.println(function.apply("abc"));
System.out.println(function.apply("xyz"));
}
}
Consumer:
Consumer is a functional interface, it will have a method “accept()”,
it is able to take an argument and it is able to process that argument
and it will not return any value.
EX:
import java.util.function.Consumer;
}
}
Supplier:
Supplier is a function interface in Java 8 version, it has a method
“get()” , it will take no argument but it will return a value.
EX:
import java.util.function.Consumer;
import java.util.function.Supplier;
}
}
EX:
import java.util.function.BiPredicate;
}
}
EX:
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
public class Main {
public static void main(String[] args) {
BiFunction<Integer, Integer, Float> biFunction = (num1,
num2) -> (float) (num1+num2)/2;
System.out.println(biFunction.apply(12,23));
}
}
DoublePredicate
DoubleFunction
DoubleConsumer
EX:
import java.util.function.DoubleConsumer;
import java.util.function.DoubleFunction;
import java.util.function.DoublePredicate;
Streams:
—-------
Stream is an Object, it is able to collect the objects from any
Collection and it is able to process those objects as per the
application requirements.
EX: To separate only even numbers from the numbers which are available
in a Collection object.
EX: To generate email Ids from the names which are available in a
Collection object.
collect():
This method is able to collect all the elements from Stream.
EX: Filter
Without Streams:
import java.util.ArrayList;
import java.util.List;
}
}
With Streams:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
}
}
EX: map:
Without Streams:
import java.util.ArrayList;
import java.util.List;
}
}
With Streams:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
}
}
count():
—---------
It is able to count the number of elements in the Stream object.
EX:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
long l = list.stream().filter(name->!
name.startsWith("s")).count();
System.out.println(l);
}
}
sorted():
It is able to sort all the elements in the Stream object in a default
sorting order.
EX:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
}
}
EX:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
}
}
EX:
import java.util.Arrays;
import java.util.List;
}
}
forEach:
It is able to apply an operation over each and every element of the
Stream.
EX:
import java.util.Arrays;
import java.util.List;
}
}
toArray():
It is able to convert all the elements to an Array.
EX:
import java.util.Arrays;
import java.util.List;
of():
It is able to create a Stream object on the basis of the elements.
EX:
import java.util.stream.Collectors;
import java.util.stream.Stream;
System.out.println(stream.collect(Collectors.toList()));
}
}
EX:
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
class Student{
private String sid;
private String sname;
private String saddr;
private List<String> coursesList;
List<List<String>> stdCoursesList =
stdList.stream().map(std-
>std.getCoursesList()).collect(Collectors.toList());
System.out.println(stdCoursesList);
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8, 10, 12, 14, 16, 18]
45
362880
1
9
Java 8 version has provided the complete predefined library for the
Date and time representation in the form of java.time package.
1. LocalDate
2. LocalTime
3. LocalDateTime
—----
If we want to get Date details from LocalDate class we have to use the
following methods.
EX:
import java.time.DayOfWeek;
import java.time.LocalDate;
System.out.println(day+"
"+date+"/"+month+"/"+year);
}
}
If we want to get time details like hour, minute, second, nano second
then we have to use the following methods from LocalTime class.
EX:
import java.time.LocalTime;
}
}
If we want to get both Date and Time values at a time we have to use
LocalDateTime class.
EX:
import java.time.LocalDateTime;
import java.time.LocalTime;
}
}
In all the above classes, we are able to represent our own Date, time
and Date time by using a static method of().
EX:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
LocalDateTime localDateTime =
LocalDateTime.of(localDate, localTime);
System.out.println(localDateTime);
}
}
java.time.Period:
It is able to represent a time duration.
To get the number of years, months and days we have to use the
following methods from Period class.
}
}
ZoneId:
—-------
ZoneId is able to represent a particular Time zone.
If we want to get the current System time then we have to use the
following method from ZoneId .
If we want to get Date and time on the basis of a particular time zone
we have to use the following method from the ZonedDateTime class.
EX:
import java.time.ZoneId;
import java.time.ZonedDateTime;