Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
93 views

Java 8 Features

This document provides examples and explanations of various Java concepts including lambda expressions, functional interfaces, method references, and streams. Some key points covered include: - Lambda expressions allow implementing functional interfaces without anonymous inner classes. They do not generate class files. - Common predefined functional interfaces include Predicate, Function, and Consumer. - Method references provide a shortcut to refer to existing methods instead of writing lambda expressions. - Streams API methods like filter(), map(), count() etc. can operate on collections and perform functional operations. - GroupBy in streams groups elements of a stream using a classifier function and performs operations on each group.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Java 8 Features

This document provides examples and explanations of various Java concepts including lambda expressions, functional interfaces, method references, and streams. Some key points covered include: - Lambda expressions allow implementing functional interfaces without anonymous inner classes. They do not generate class files. - Common predefined functional interfaces include Predicate, Function, and Consumer. - Method references provide a shortcut to refer to existing methods instead of writing lambda expressions. - Streams API methods like filter(), map(), count() etc. can operate on collections and perform functional operations. - GroupBy in streams groups elements of a stream using a classifier function and performs operations on each group.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Introduction || Session - 1

 Example 1:

 Example 2:

 Example 3:

Lambda Expression Functional Interface|| Session - 2 


 Valid interfaces

 Valid overriding method

 B interface is declared as functional interface and two methods indirectly. So invalid for B.



 Parent Reference can hold child object is valid.
 Lambda expressions not replacement for .class files. They does not generate .class file.
Anonymous Inner Class vs Lambda Expressions || Session - 4 
 Default method() & Static method() inside Interface
 Why default method is called defender or what is the reason to introduce default method?
 When a new method is added then all the impl class will be effected. So it is optional and dummy
impl is in interface.

 Default not allowed in classes. If same syntax while impl class then CE says “default not allowed in
classes only in interfaces”.
 Object class methods are not allowed in interface. Since already available in Object class. All the
classes are sub classes of Object class.

 Diamond Problem and Solution: Two interfaces with default implementation then implementation
class should override that method.
 Call interface method from implemented class
 If only static method better for interface instead of class. Since static method not required object
 Interface static method should call using interface name only. Since interface static methods are not
available to the implementation classes.

 Main method is allowed in interface from 1.8 Version because static methods are allowed.

 If every method is static in a class then why to go for class is heavy weight.
 For general utility methods go for interface instead of class.
 Predefined Functional Interface: Predicate

 For conditional check use predicate


Predefined Functional Interface: Predicate, Function & Consumer 

 Functional Interface:
 Consumer:
 Consumer Chaining:

Supplier,BiFunction,BiPredicate & Biconsumer 


 6 Digit OTP

 BiPredicate:

BiConsumer:
 Note: BiSupplier is not there
 Problem for primitive Functional interface: As per the below example there is N times auto boxing
auto unboxing need to happen in for loop.
It will impact the performance.
 3 types of interfaces based on primitive types int/double/long and method name standards are
based on return types. (AsInt Int is return type for that FI).
 If input and return types are same then don’t use function go for UnaryOperator.
 Where ever run method is required use already implemented m1 method.
 Advantage of Method Reference: Code Reusability.
 Syntax for Static and non-static reference:
Rule:
The arguments should match in method reference
Access specifiers not considered

 Return type change not considered.

 Which method referring which method?


 Here add method referring sum method.
 Here interface get method referring the Sample class constructor. So referring constructor.

Arguments should match.


 Even default constructor is there. Constructor reference automatically identify Parameter matching
constructor for its reference.

 Imagine without constructor Reference, how to write the code


 With Lambda expressions:

 These are the advantages of Constructor Reference(Code to create object and return that object).

Filter:

 Map method:
 Failed students count:

 Whenever we are talking about min and max, it needs to be sorted either in ascending or descending
order.

 Sub packages classes are not available to parent package. Inheritance not applicable in packages.
 If you want to convert into collections use Arrays.
 Date and Time New features:
To get current date and current system time.
Output:
 Group By in Streams:

java.lang.OutOfMemoryError: PermGen space


JVM verbose GC analysis

Local variables are minor collections are comes under young generation.

You might also like