3. Java provides a facility to create default methods inside the interface.
Java Default Methods
4. ➢ Improve scalability.
➢ Added new method without hampering current implementation.
➢ Implementation flexibility.
Why Default Method?
5. What-if the multiple interfaces have default methods with the same
signatures.
Rule 1 – Classes take higher precedence than interfaces
Default method conflict
6. • Rule 2 – Derived interfaces or sub-interfaces take higher precedence than
the interfaces higher-up in the inheritance hierarchy.
Default method conflict
In the above class diagram, interface B inherits from interface A. Both have a default method
print() with the same signature. Class C implements both interfaces A & B. When print()
method is invoked on an instance of class C then the implementation in interface B is invoked
as it is the lowest child/most derived interface in the inheritance hierarchy.
7. • Rule 3 – In case Rule 1 and Rule 2 are not able to resolve the conflict then the
implementing class has to specifically override and provide a method with the
same method definition.
Java 8 Features
In the above class diagram, class C inherits from interfaces A & B, both of which have the
default implementations of print(). Since, both interfaces A & B are parents of C, they are at
the same hierarchy level, and hence, C has to provide its own implementation of method
print().
B.super.print()
8. • Scenario 1 of diamond problem in Java 8
Diamond Problem
Java 8 resolves this situation by considering that there is only one implementation of
print() method, which is in class Alpha. Hence, when Delta invokes print() then the
implementation of print() in Alpha is executed.
9. • Scenario 2 of diamond problem in Java 8 –
Diamond Problem
The answer lies in Rule 3 of the conflict resolution
10. Lambda Overview
Why Lambdas?
➢ Enable functional programming.
➢ Readable and concise code.
➢ Easier to use APIs and libraries.
➢ Enables support for parallel processing.
12. Inline values
String name = “SK Paik";
int age = 32;
➢Can we assign a block of code to a variable as values?
➢Can we do something that?
<Interface> aBlockOfCode = {
................
................
}
Yes by lambda we can do that.
13. Expression
Methods
public void greet()
{
System.out.print(“Hello”);
}
public int add(int a, int b)
{
return a+b;
}
publuc int getLength(String s)
{
return s.lenght();
}
Lambda Expression
greetExp = ()->System.out.print(“Hello”);
addExp = (int a, int b)->return a+b;
addExp = ( a, b)->return a+b;
lengthExp = (String s) ->s.lenght();
divExp = (int a, int b)->{
if(b==0) return 0;
return a/b;
}
14. Type Inference
Interface Greet{
void greet();
}
Interface Add{
Int add(int a, int b);
}
Interface Length{
Int getLength(String s);
}
Lambda Expression of above interface
Greet greetExp = ()->System.out.print(“”)}
Add addExp = (int n,int m)->n+m;
Length lenghtExp = (String s)->s.lenght();
15. Runnable In Lambda
Public void simpleThread(){
Thread thread = new Thread(new Runnable(){
Public void run(){
System.out.println(“Hello From Runnable”);
}
});
Thread.start();
}
Public void simpleThread(){
Thread thread = new Thread(()->System.out.print(“Hello lambda));
thread.start();
}
16. Stream represents a sequence of objects from a source, which supports
aggregate operations. Following are the characteristics of a Stream.
• Sequence of elements − A stream provides a set of elements of specific
type in a sequential manner. A stream gets/computes elements on
demand. It never stores the elements.
• Source − Stream takes Collections, Arrays, or I/O resources as input
source.
• Aggregate operations − Stream supports aggregate operations like filter,
map, limit, reduce, find, match, and so on.
• Pipelining − Most of the stream operations return stream itself so that
their result can be pipelined.
• Automatic iterations − Stream operations do the iterations internally over
the source elements provided, in contrast to Collections where explicit
iteration is required.
Java Stream
17. • Before we look into Java Stream API Examples, let’s see why it was
required. Suppose we want to iterate over a list of integers and find out
sum of all the integers greater than 10.
Java Stream
1. Program is handling the algorithm to
iterate over the list.
2. The program is sequential in nature,
there is no way we can do this in parallel
easily.
3. There is a lot of code to do even a simple
task.
19. • Collections:
A collection is an in-memory data structure to hold values and before we
start using collection, all the values should have been populated.
• Java Stream
Whereas a java Stream is a data structure that is computed on-demand. Java
Stream doesn’t store data, it operates on the source data structure (collection and
array) and produce pipelined data that we can use and perform specific
operations.
Collections and Java Stream
20. With Java 8, Collection interface has two methods to generate a
Stream.
• stream() − Returns a sequential stream considering collection
as its source.
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
• parallelStream() − Returns a parallel Stream considering
collection as its source.
Stream Type
21. • We can use Stream.of() to create a stream from similar type
of data.
Creating Java Streams
• We can use Stream.of() with an array of Objects to return the stream.
• We can use Collection stream() to create sequential stream and parallelStream() to
create parallel stream.
22. Creating Java Streams
We can use Stream.generate() and Stream.iterate() methods to create Stream.
We can use java Stream collect() method to get List, Map or Set from stream.
24. • Stream filter() example: We can use filter() method to test stream elements for a
condition and generate filtered list.
Java Stream Intermediate Operations
25. Java Terminal Operations
• Stream count() example: We can use this terminal operation to count the
number of items in the stream.
26. Short-circuit operation
Java 8 short-circuiting operations are just like boolean short-circuit evaluations in
Java.
Java 8 Stream short-circuit operations are not limited to boolean types. There are pre
defined short-circuiting operations.
Intermediate short-circuiting methods
Stream<T> limit(long maxSize)
Terminal short-circuiting methods
Optional<T> findFirst()
27. Stream pipelines
• Expressing a stream pipeline as a series of functional transformations enables
several useful execution strategies, such as laziness, parallelism, short-circuiting,
and operation fusion.
28. • Not Reusable: Once a Stream is consumed, it can’t be used later
on. As you can see in above examples that every time I am creating
a stream.
• Performance: A for loop through an array is extremely lightweight
both in terms of heap and CPU usage. If raw speed and memory
thriftiness is a priority, using a stream is worse.
• Familiarity. The world is full of experienced procedural
programmers, from many language backgrounds, for whom loops
are familiar and streams are novel. In some environments, you
want to write code that's familiar to that kind of person.
• Debuggers are improving, but even now, when you're stepping
through stream code in a debugger, it can be harder work than the
equivalent loop, because a simple loop is very close to the
variables and code locations that a traditional debugger works
with.
Stream API Limitations