0% found this document useful (0 votes)
5 views

Java Unit 3 Notes New Fetures Part 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Unit 3 Notes New Fetures Part 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Java 8 New Striking Features

Java programming language is always a leading programming language, from the last 20 years Java is
the winner is all aspects, but nowadays functional programming concepts are on air. That’s why
Python, Ruby, NodeJS, and other programming languages that support functional programming was
taking lead from Java, so to take lead again, Java introduced new striking features in 1.8 version that
highly support functional programming in Java. It does not mean, Java does not object orient now, it
is object-oriented but support functional programming being object-oriented.

These are the new striking features in Java 1.8 version:

1. Default method in Interface


2. Static method in Interface
3. Functional Interface
4. Lambda Expression
5. Method References
6. Java.util.function packages for Functional Interface
7. Java Stream API

Default Method in Interface


In Java earlier versions, before 1.8, it was not possible to create any method with a body in Interface,
the only abstract method with public access modifier was supported, but now from version 1.8, it is
possible to declare default method that has body also.

If the programmer wants to add new service or logic in the interface without doing modification on
the implementation class of interface, then now from Java version 1.8 he/she can use default methods
in the interface.

Let’s understand by example:

interface Message {
public abstract void msg ();

public default void msg2()

System.out.println(“Default method in Interface”);

public class Main implements Message {

public void msg ()

System.out.println(“This is Functional Interface Method”);

}
public static void main (String [] args)

Message m1 = new Main ();

m1.msg();

m1.msg2();

Points to be Remember of Default Members:

1. Any number of default method can be declared in the interface


2. A default method can be overload or override
3. We cannot override java.lang.Object method in the interface as default methods.

Static Method in Interface


In earlier versions Java, before 1.8, it was not possible to create a static method in Interface, the only
public abstract method was supported, but now from version 1.8, it is possible to declare static
method also.

Let’s understand by example:

interface Message {
public abstract void msg ();

public default void msg2()

System.out.println(“Default method in Java”);

public static void msg3()

System.out.println(“Static method in Interface”);

public class Main implements Message {

public void msg ()

System.out.println(“This is Functional Interface Method”);

}
public static void main (String [] args)

Message m1 = new Main ();

m1.msg();

m1.msg2();

Message.msg3();

Functional Interface
Functional Interface is new concepts in Java after version 1.8, It is the core striking features in version
1.8, because of a functional interface, other features of java 1.8 exists like Lambda expression, method
references, Java.util.function packages and Java Stream API. All these features are 100% dependent
on functional interface. This functional interface concept brings again Java to stand strongly in front
of the functional programming supported languages like Python, Ruby, etc.

It is not a new kind of interface; it is an old one but with some restrictions. An Interface, which allows
only one abstract method declaration with @FunctionalInterface annotation is called Functional
Interface. In Function Interfaces, more than one default, or static method is allowing and you can also
write java.lang.Object class methods, but more than one abstract method is not allowed.

Let’s understand it by example:

@FunctionalInterface
interface Message {
public abstract void msg ();

public class Main implements Message {

public void msg ()

System.out.println(“This is Functional Interface Method”);

public static void main (String args )

Message m1 = new Main ();

m1.msg();
}

Advantages of Functions Interfaces:

1. To develop function programming in Java


2. To develop lambda expression in Java
3. To develop method references in Java

Lambda Expression
Lambda expression is a great feature in Java 1.8 version. It helps the programmer to write less code
with functional programming features. Lambda Expression is just an anonymous or nameless function
declaration, it means the function which doesn’t have a name, return type, and access modifiers.

Let’s understand it by examples:

A) Lambda expression or nameless function declaration with zero parameter

Lambda Expression Function Declaration:

() -> System.out.println(“This is lambda expression function declaration”);

Normal Function Declaration:

public void Message ()

System.out.println(“This is normal function declaration”);

B) Lambda expression or nameless function declaration with one parameter

Lambda Expression Function Declaration:

Lambda expression function with one parameter can be declared 3 ways as explained below.

Type 1 declaration:

(String msg) -> System.out.println(msg);

Type 2 declaration:

(msg)->System.out.println(msg);

Type 3 declaration:

msg->System.out.println(msg);

Normal Function Declaration:

public void Message (String msg)

System.out.println(msg);
}

C) Lambda expression or nameless function declaration with more than one parameters

Lambda Expression Function Declaration:

Lambda expression function with more than one parameter can be declared only 1 way as
explained below.

(int num1, int num2) -> System.out.println(“Sum of two number is=” + (num1+num2));

Normal Function Declaration:

public void sum (int num1, int num2)

System.out.println(“Sum of two number is=” + (num1+num2));

D) Lambda expression or nameless function declaration with multiple statements. If more than
one statements present than we have to enclose inside curly braces. If one statement
present, then curly braces are optional as shown above also.

Lambda Expression Function Declaration:

Lambda expression function with more than one parameter can be declared only 1 way as
explained below.

(int num1, int num2) -> {

Int sum=num1+num2;

System.out.println(“Sum of two number is=” + sum);

Normal Function Declaration:

public void sum (int num1, int num2)

Int sum=num1+num2;

System.out.println(“Sum of two number is=” + sum);

Let’s take a complete program to understand Lambda Expression:

@FunctionalInterface
interface Message
{

public abstract void msg();

}
public class Test

public static void main(String args[])

//Interface implementation using Anonymous Inner Class: - Order Method in Java 1.7 or earlier

Message m1 = new Message()

@Override
public void msg()
{

System.out.println(“By using Anonymous class”);

m1.msg();

//Interface implementation using Lambda Express: - New method in Java 1.8 or later

Message m2 = ()->System.out.println(“By using Lambda Express”);

m2.msg();

Lambda Expression Advantages:


1. It reduces the length of the code so that readability of the code will be improved
2. It resolves the complexity of anonymous inner classes
3. It can be pass as arguments to methods.
4. It can be providing Lambda Expression in the place of an object

Function Interface vs Lambda Expressions:


1. Functional Interface is required for writing a single Lambda expression program, without
Functional Interface, one cannot write any Lambda expression program in Java.
2. But for the Functional Interface program, you can write either with Lambda Expression or
without Lambda Expression. So for Functional Interface Lambda expression is not compulsory.

Method References
The mapping of Function Interface method to the specified method by using “::” (double colon)
operator is called Method Referencing. This specified method can be either a static or instance
method. But there is a condition, Functions Interface method and specified method should have same
argument types, except this the remaining things like return type, method name, access modifier is
not required to match.
The syntax for Method References:

A) If a specified method is a static method:

Classname::methodName

B) If a specified method is an instance method:

Objectname::methodName

Let’s understand with an example:

@FunctionalInterface

interface Message

public abstract void msg();

class DisplayMessage

public void display()

System.out.println("This is method references example");

public static void display2()

System.out.println("This is method references example");

public class Test

public static void main (String [] args)

DisplayMessage dm = new DisplayMessage();

Message m1 =dm::display();

m1.msg();

Message m2 =DisplayMessage::display2();
m2.msg();

Constructor References
The mapping of the constructor to the specified method by using “::” (double colon) operator is called
Constructor Referencing.

The syntax for Constructor References:

Classname::new

Let’s understand with an example:

@FunctionalInterface

interface Message

public abstract void msg ();

class DisplayMessage

public DisplayMessage()

System.out.println("This is method references example");

public class Test

public static void main (String [] args)

Message m1 =DisplayMessage::new;

m1.msg();

}
Use of Package java.util.function
This java.util.function package provides standard library-based functional interfaces for common
requirements with their corresponding lambda expression, which can be used by the programmer in
his code instead of creating brand new functional interfaces.

List of most commonly used Functional Interfaces under this package are:

1. Function
This interface has one function apply (), this function takes one input parameter as T and
return value as R after performing some kind of operation on the input parameter.
Structure of Function interface:
@FunctionalInterface
public interface Function <T, R> {
R apply (T t);
}
This T and R may have any type of value like Integer, Float, Double, String, etc.
Let’s understand the use of it by given example:

import java.util.function.Function;

public class Main {

public static void main (String args[])

// Function which takes in a number and returns half of it

Function<Integer, Double> half = a -> a / 2.0;

// apply the function to get the result

System.out.println(half.apply(10));

//One more use of it, to get square of an integer

Function<Integer, Integer> square= num1-> num1*num1;

System.out.println(“Square of number is =” +square.apply(4));

2. BiFunction
This interface also has one function apply (), this function takes two input parameters as T and
U and returns a value as R after performing some kind of operation on given input parameters.
Structure of BiFunction interface:
@FunctionalInterface
public interface BiFunction <T, U, R> {
R apply (T t, U u);
}
This T, U, and R may have any type of value like Integer, Float, Double, String, etc.
Let’s understand the use of it by given example:

import java.util.function.BiFunction;

public class Main {

public static void main (String args[])

// This function takes two numbers and returns sum of these two number

BiFunction<Integer, Integer, Integer> sum = (Integer num1, Integer num2) -> num1+num2;

// apply the function to get the result

System.out.println(“Sum of two number is =” +sum.apply(10,50));

3. UnaryOperator
This interface also has one function apply(), this function takes one input parameter and
returns a value of the same input type after performing some kind of operation on the given
input parameter.
Structure of Function interface:
@FunctionalInterface
public interface UnaryOperator <T> {
T apply (T t);
}
This T may have any type of value like Integer, Float, Double, String, etc.
Let’s understand the use of it by a given example:

import java.util.function.*;

public class Main {

public static void main (String args[])

//This take one integer parameter and return square of an integer

UnaryOperator<Integer> square= num1-> num1*num1;

System.out.println(“Square of number is =” +square.apply(4));

4. Consumer
This interface has one function accept (), this function only takes one input parameter, but
does not return any value.
Structure of Function interface:
@FunctionalInterface
public interface Consumer <T> {
void accept (T t);
}
This T may have any type of value like Integer, Float, Double, String, etc.
Let’s understand the use of it by a given example:

import java.util.function.*;

public class Main {

public static void main (String args[])

// Function which takes in a number and returns half of it

Consumer<Integer> square = num1 -> {

System.out.println(“Square of number is =” +(num1*num1));

};

// accept function to get the result

square.accept(10);

5. BiConsumer
This interface has one function accept(), this function takes two input values for performing
some operations and does not return any value.
Structure of Function interface:
@FunctionalInterface
public interface BiConsumer <T, R> {
void accept (T t, R r);
}
This T and R may have any type of value like Integer, Float, Double, String, etc.
Let’s understand the use of it by a given example:

import java.util.function.*;

public class Main {

public static void main (String args [])

// This function takes two input value and does not return any value

BiConsumer<Integer, Integer> sum = (Integer num1, Integer num2) -> {

System.out.println(“Sum of two number are =” +(num1+num2));


};

//accept function to get the result

sum.accept(30,40);

6. Supplier
This interface has one function get(), this function does not take any parameter, but return
value after performing a given task of the function.
Structure of Function interface:
@FunctionalInterface
public interface Supplier<T> {
T get ();
}
This T may have any type of value like Integer, Float, Double, String, etc.
Let’s understand the use of it by a given example:

import java.util.function.*;

public class Main {

public static void main (String args [])

// This function does not take any input value, but return value

Supplier<String> s1 = () -> {

System.out.println(“Enter your name=>”);

Scanner scan=new Scanner(System.in);

String name=scan.nextLine();

return name;

};

//get function to get the result

System.out.println(“Your name is =” +s1.get());

Java Stream API


Stream API is another new feature of Java 8. All the classes and interfaces of this Stream API is in the
java.util.stream package. By using this package, the programmer can perform various aggregate
operations on the data returned from collections, arrays, and I/O operations.
Let’s understand it with examples:

1. Use of Stream API map () function to get square of each number present in ArrayList.

import java.util.function.*;

public class Main {

public static void main (String args [])

List<Integer> li = new ArrayList<Integer> ();

li.add(10);

li.add(20);

li.add(30);

li.add(40);

List<Integer> ll =li.stream().map((x)->x*x).collect(Collectors.toList());

System.out.println(“List of number are=”+li);

System.out.println(“Square of list of number are=”+ll);

Explanation of code:

This stream () function converts all list elements into a stream, then map () function perform a square
operation on each element of a list, and then at last collect () function again convert all stream
elements to list elements.

2. Use of Stream API filer () and map () function to get square of selected number present in
ArrayList.

import java.util.function.*;

public class Main {

public static void main (String args [])

List<Integer> li = new ArrayList<Integer> ();

li.add(10);

li.add(11);

li.add(30);

li.add(15);

List<Integer> ll =li.stream().
filter((x)->x%2==0).

map((x)->x*x).

collect(Collectors.toList());

System.out.println(“List of number are=” +li);

System.out.println(“Square of list of number are=” +ll);

Explanation of code:

This stream () function converts all list elements into stream, then filter () function select only even
numbers the list as per the given condition for even number, then map () function perform a square
operation on filtered even number of the list, and then at last collect () function again convert all
stream elements to list elements.

3. Use of Stream API distinct(), limit(), peek() and count() function to get square of selected
number present in ArrayList.

import java.util.function.*;

public class Main {

public static void main (String args [])

List<Integer> li = new ArrayList<Integer> ();

li.add(10);

li.add(10);

li.add(30);

li.add(30);

li.add(20);

li.add(20);

li.add(50);

li.add(60);

System.out.println(“List of number are=” +li);

li.stream().distinct().limit(3).

peek((x)->{System.out.println(x+” square =”+(x*x));}).

count();
}

Explanation of code:

This stream () function converts all list elements into a stream, then distinct () function select unique
numbers from the list, then limit () function select first numbers from the list as per parameter
assigned in limit() function like 3 in above example. then peek() function perform a task on selected
numbers from limit() function and then count function display selected numbers of list line by line.

There are lot more functions in this Stream API, like anyMatch(), allMatch(), noneMatch(), findAny(),
findFirst(), toArray(), reduse(), max(), min(), and etc. Details of all these functions are lot of the scope
of this app.

Java Stream API Features:

1. Stream does not store the elements; it simply performs the aggregate operations on data.
2. The aggregate operations that perform on the collection array or any other data source do
not change the data of the source, they simply return new steam of data.
3. All the stream operations are lazy which means they are not executed until they are needed.

You might also like