0% found this document useful (0 votes)
4 views7 pages

Java 8 Material

Uploaded by

siva kick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views7 pages

Java 8 Material

Uploaded by

siva kick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Java8 Features:

1forEach() with Functional interface

2) default and statie methods in interfaces

3) Functional interfaces and Lamda Expressions

4Java Stream API


5) Java Time API

6)Collection API improvements

7)Concurrency APl improvements

8)Java 10 improvements

2) default and static methods in interfaces

In java8 interface can allow default & static methods

I f you want to use interface default method you can use, because
it contains body.
I f you want you can override
To achieve the diamond problem, we can use default method
When you implemented the class with multiple interfaces, you must
should overide and call interface default method
Static methods are we can call individually required interface static
methods
You can't override the static methods in interface

public class Employee implements Manager, TeamLead{

@Override
public void workingHours () {
System.out.println("9 working hrs. ")

@Override
public String role() {
return Manager.super.role()

public static void main(Stringl] args) {


Manager ma=new Employee();
ma.workingHours ();
System.out.println (ma.role() );
System.out.println(Manager.project());
System.out.println(TeamLead.project());

public interface Manager


public void workingHours ();

default public String role(){


return "I'm manager";

static String project() {


return "Yesbank";

public interface TeamLead


public void
workingHours();
default public String role()
return "I'm TeamLead":

static String project() {


return IndusIndbank";

5) Java Time API

before java 8 if you are using Date class, you will get the entire time zone

but in Java8 if you want you can print the date


tf you want only time you print only time

Ifyou want both, you can print both


By using DateTime APl you can fetch the data based on zones also

If you provided non leap year date, itself identify the problem and give the Date TimeExcpetion.
Date date= new Date();
System.out.println(date):
LocalDate ld=LocalDate.now()
System.out.println(ld);
LocalDate ldo=LocalDate.of(1997, Month.FEBRUARY,
29)://it will give Date Time Exception
System.out.println(ldo);
LocalTime lt=LocalTime.now);
System.out.println(1t):
LocalDateTime ldt=LocalDateTime.now)
System.out.println(ldt);
Set String> zones=ZoneId.getAvai labLeZoneTds Q:
zones.stream().forEach (System.out::println)i
LocalTime
ldof=LocalTime.now(ZoneId.of("Australia/West ") );
System.out.println(ldof);
Lamda:

-To achieve the functional programming will use lamda


n o need to provide Object or new constructor to lamda
w e no need to provide method and return type
we no need to provide curly braces
we no need to provide method parameter dataType, if it is single
parameter
>if we have single parameter we no need to provide parameter
brackets

public interface Nagaraj


public abstract void mySelf(String name);
public static void main(String[] args) {
Nagaraj nagl=new Nagaraj(){
@Override
public void mySelf(String name) {
System.out.println("Nagaraj "+name )5

nag1.myself("s");
//using Lamda
Nagaraj nag-s-> System.out.println("Nagaraj "+s) ;
nag.mySelf("s");
11- lamda expression
//O -2 {body

Functional Interface:

The iterface should annotated with


@FunctionalInterface
>It allows only one abstract method
But allows many default and static methods
For that we need to import java.util.funtion package.
Functional interfaces are Consumer, Supplier, predicate,
Function, Runnable, Comparator, Callable

Consumer
i t is a functional interface
i t takes input, but won't return any thing
i t have accept(T)
Consumer <Integer2consumer=i->System.out.println("this is
Consumer "+i);
consumer. accept(10);

Supplier:
which isfunctional interface
i t won't take any input but return some value
i t contains get)
Supplier<String> supplier=()-> "Teja Supplier";
String val=supplier.get();
System.out.println(val);
Predicate:

i tisfunctional interface
i t contains test()
i t returns always Boolean value
Predicate<String> predicate=5-> s.length()==7;
Boolean data=predicate.test("Teja IT");
System.out.println(data);
Function:
i t will take some input and Return someValue

i t contains apply(T
inputVal, ReturnVal

Double fun=i-> i/2.5;


Function<Integer,
Double funData=fun.apply(20);

System.out.println(funData )
Stream API:
t is used to process the collection of Objects
Stream will process the sequence of Objects and
it can be pipelined to
get the required result
Streams doesn't change original data, it will pipeline the data only by
using methods, like filter, map, distinct etc.,
t doesn't store any data
It will useful to do functional programming, using lamda expression
Streams will support both parallel and sequential processing
in
A l l the Java stream API interfaces and classes
are

java.util.stream package
Collection AP:

It stores the data and holds the data in data Structures, like list, set,map,Queue
I t won't use functional interfaces
W e can manipulate the Collection Data by using add(), or remove(0
It can be traversable multiple times without creating it again and again

I t supports parallel processing

IntermediateOperations:
>All intermediate operations return Stream (can be chained)

filter(Predicate<T>)
map(Function<T>)
flatMap(Function<T)
sorted(Comparator<>)
peek(Consumer<T>)
distinct()
limit(long n)
skip(longn)

Terminal Operations:
Terminal operations produces a non-stream (cannot be chained)

forEach
forEachOrdered

toArray
reduce
collect
min
max
count
anyMatch
allMatch
noneMatch
findFirst
findAny
Method Reference:

We can denotes with(double colon)


It is used to refer the functional interface method
Instead of using lamda to refer the method you can use, method reference
ClassName : methodName
max(Comparator.comparina(i->Integer.value0f(i)))

max(Comparator.comparing(Integer::value0f))
Optional
Optional is a class, which was introduced in java8
>Which was to handle the null data
I t deals with nullpointer exception
>Which contains, Optional.emptyl), isPresent().get(),orElse etc., methods

String val=null;
if(vallenull) i
else
val.toupperCase()i
val-"Teja IT"s

System.out.println(val.toupperCase()):// get null exception

1/ if you handle with optional


Optional<String> optString=Optional.empty();
System.out.println(optString);
optString-Optional.ofNullable(val) ;
optstrins-isPresent 0i//tru or false

System.out.println(optstring-get());
System.out.printIn(optString.orElse("Teja IT").toUpperCase()):
Map:
It will work on only List of data
Map operation will return single value for single input
Produces a Stream of values
Only performing on mapping
Only OneToOne mapping occurs in map
ListInteger> listi=Arrays.astist(1,3,5,7);
ListIntegers list2=Arrays . aslist (2,4,5,6);
ListeInteger> 1ist3-Arrays.astist(4,9,8, 7);

ListeList<Integer>> listpata=Arrays. aslist(listi,list2,list3);

ListestreamcInteger>s streanMap-listData.stream()
. map(x2x.stream())
collect (Collectors.tolist0):
ystem.out.printIn(streamMap )

FlatMap:
>It will work on List of List data

List«List<Integer>»
It will perform mapping and flattening as well
Produce Stream of Stream data
OneToMany mapping occurs in faltMap()

ListIntegers list1-Arrays.aslist(1,3,5,7);
List Integer> list2=Arrays.aslist(2,4,5,6);
List<Integer> 1ist3=Arrays. aslist (4,9,8,7);
List<List< Integer>> listdata-arrays.aslist (1isti, list2, list3) ;

List<Integer> streamMap-listData.stream()
flatMap(x-2x.stream())
.collect(Collectors.tolist ());
System.out.println(streamMap)

You might also like