Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Powerpoint Templates
Page 1
Powerpoint Templates
Java Lambdas & Streams
By Mallik
Powerpoint Templates
Page 2
Java 8 features
•Define methods in interface
•Functional programming
•Functional Interface
•Lambda Expressions
•Stream API
Powerpoint Templates
Page 3
Methods In Interface
•Interface is to define the type of the class
•Interface methods are by default public abstract in java.
•There are two ways we can define interfaces
Default methods (DefaultMethodDemo.Java)
Static methods (InterfaceStaticMethod.Java)
Powerpoint Templates
Page 4
Functional Programming
•Code in declarative style
•Solves the concurrency issues
•Simple, readable and testable
•Lazy evaluation advantage
•No assignments( no mutability)
•No external dependency
•Always return the same result for same input
total
MyThread1 MyThread2 MyThred3
Total variable in the object is shared among the multiple threads.
To avoid the concurrency issues we may place synchronized block, it
allows other Threads to wait.
Powerpoint Templates
Page 5
• Object Oriented Programming – allows mutability
• Functional programming – doesn’t allow mutability.
• Java Streams handles the concurrency isses.
• How to avoid the Concurrency issues
– making the variable as final.
– To do any changes to the final variable take the copy of the
variable and do operations.
AnyObject method(AnyObject o)
{
AnyObject a=new AnyObject();
a.state=o.getState()*2;
Return a;
}
Powerpoint Templates
Page 6
Functional Interfaces
• An interface with only one abstract method is a
functional interface.
• Without Lambdas
. With Lambdas
JButton button = new JButton("Click Me!");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("Handled by anonymous class
listener"); }});
button.addActionListener(e -> {
System.out.println("Handled Lambda listener");
System.out.println("Have fun!");
});
Powerpoint Templates
Page 7
Lambda Expressions
Lambda expressions are similar to methods, it has arguments, a
body and return type. They can also be called as anonymous
methods. A method without name.
Structure of a Lambda Expression
-(Argument List) Arrow Token {Body }
(type argument, ….) –> { java statements; }
Example Lambda Expressions
() -> { System.out.printlns("Hello World!");}
(int a, int b) -> a + b
() -> { return 1; }
(String name) -> { System.out.println("Hello "+name); }
n -> n % 2 != 0
Examples we see in the code
We can use Lambda expressions in functional interfaces
(Package java.util.function)
Powerpoint Templates
Page 8
Stream API
•Processing large chunk of data in parallel streams help to
leverage multi-core architecture with out multi thread code.
•Write the code in declarative way which keeps the code simple ,
reliable and still lot efficient in background.
•Problems of concurrency on shared variables will be avoided in
streams.
•Stream represents a sequence of objects from a source, which
supports aggregate operations.
•two different kinds of Streams
•Streams( supported by stream)
•parallelStream( to create parallel streams)
•Streams supports a pipe line, which is the sequence of
aggregate operations.
•Streams provide intermediatery and Terminal operators
•Once stream is used we can’t reuse it.
Inermediate (lazy) Terminal
Filter findFirst()
Map forEach()

More Related Content

Lambdas

  • 1. Powerpoint Templates Page 1 Powerpoint Templates Java Lambdas & Streams By Mallik
  • 2. Powerpoint Templates Page 2 Java 8 features •Define methods in interface •Functional programming •Functional Interface •Lambda Expressions •Stream API
  • 3. Powerpoint Templates Page 3 Methods In Interface •Interface is to define the type of the class •Interface methods are by default public abstract in java. •There are two ways we can define interfaces Default methods (DefaultMethodDemo.Java) Static methods (InterfaceStaticMethod.Java)
  • 4. Powerpoint Templates Page 4 Functional Programming •Code in declarative style •Solves the concurrency issues •Simple, readable and testable •Lazy evaluation advantage •No assignments( no mutability) •No external dependency •Always return the same result for same input total MyThread1 MyThread2 MyThred3 Total variable in the object is shared among the multiple threads. To avoid the concurrency issues we may place synchronized block, it allows other Threads to wait.
  • 5. Powerpoint Templates Page 5 • Object Oriented Programming – allows mutability • Functional programming – doesn’t allow mutability. • Java Streams handles the concurrency isses. • How to avoid the Concurrency issues – making the variable as final. – To do any changes to the final variable take the copy of the variable and do operations. AnyObject method(AnyObject o) { AnyObject a=new AnyObject(); a.state=o.getState()*2; Return a; }
  • 6. Powerpoint Templates Page 6 Functional Interfaces • An interface with only one abstract method is a functional interface. • Without Lambdas . With Lambdas JButton button = new JButton("Click Me!"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.out.println("Handled by anonymous class listener"); }}); button.addActionListener(e -> { System.out.println("Handled Lambda listener"); System.out.println("Have fun!"); });
  • 7. Powerpoint Templates Page 7 Lambda Expressions Lambda expressions are similar to methods, it has arguments, a body and return type. They can also be called as anonymous methods. A method without name. Structure of a Lambda Expression -(Argument List) Arrow Token {Body } (type argument, ….) –> { java statements; } Example Lambda Expressions () -> { System.out.printlns("Hello World!");} (int a, int b) -> a + b () -> { return 1; } (String name) -> { System.out.println("Hello "+name); } n -> n % 2 != 0 Examples we see in the code We can use Lambda expressions in functional interfaces (Package java.util.function)
  • 8. Powerpoint Templates Page 8 Stream API •Processing large chunk of data in parallel streams help to leverage multi-core architecture with out multi thread code. •Write the code in declarative way which keeps the code simple , reliable and still lot efficient in background. •Problems of concurrency on shared variables will be avoided in streams. •Stream represents a sequence of objects from a source, which supports aggregate operations. •two different kinds of Streams •Streams( supported by stream) •parallelStream( to create parallel streams) •Streams supports a pipe line, which is the sequence of aggregate operations. •Streams provide intermediatery and Terminal operators •Once stream is used we can’t reuse it. Inermediate (lazy) Terminal Filter findFirst() Map forEach()