Java 8 Material
Java 8 Material
8)Java 10 improvements
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
@Override
public void workingHours () {
System.out.println("9 working hrs. ")
@Override
public String role() {
return Manager.super.role()
before java 8 if you are using Date class, you will get the entire time zone
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:
nag1.myself("s");
//using Lamda
Nagaraj nag-s-> System.out.println("Nagaraj "+s) ;
nag.mySelf("s");
11- lamda expression
//O -2 {body
Functional Interface:
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
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
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:
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(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);
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)