JAVA Module 5
JAVA Module 5
Java
Module 5
07/02/2024 1
Module 5: Java 8 Features
• Base 64 encoding/decoding
• Default and static methods in
Interfaces
• Functional Interfaces and Lambda
Expressions
• Streams
07/02/2024 2
1. Java Base64 Encode and
Decode
• Java provides a class Base64 to deal with encryption. You can
encrypt and decrypt your data by using provided methods.
You need to import java.util.Base64 in your source file to use
its methods.
• Base64.Decoder:
• This class implements a decoder for decoding byte data
using the Base64 encoding scheme as specified in RFC
4648 and RFC 2045.
• Base64.Encoder:
• This class implements an encoder for encoding byte data
using the Base64 encoding scheme as specified in RFC
4648 and RFC 2045.
07/02/2024 5
Base64 Methods
• public static Base64.Decoder getDecoder()
• public static Base64.Encoder getEncoder()
• public static Base64.Decoder getUrlDecoder()
• public static Base64.Encoder getUrlEncoder()
• public static Base64.Decoder getMimeDecoder()
• public static Base64.Encoder getMimeEncoder()
07/02/2024 6
Java Base64 Example: URL Encoding and Decoding
• import java.util.Base64;
• public class Base64BasicEncryptionExample {
• public static void main(String[] args) {
• // Getting encoder
• Base64.Encoder encoder = Base64.getUrlEncoder();
• // Encoding URL
• String eStr = encoder.encodeToString("http://www.sinhgad.edu".getBytes());
• System.out.println("Encoded URL: "+eStr);
• // Getting decoder
• Base64.Decoder decoder = Base64.getUrlDecoder();
• // Decoding URL
• String dStr = new String(decoder.decode(eStr));
• System.out.println("Decoded URL: "+dStr);
• } }
• Output:
• Encoded URL: aHR0cDovqYXZhLXR1dG9yaWFsLw==
• Decoded URL: http://www.sinhgad.edu
07/02/2024 7
2. Default and static methods in
Interfaces
• Java provides a facility to create default methods
inside the interface. Methods which are defined inside
the interface and tagged with default are known as
default methods. These methods are non-abstract
methods.
07/02/2024 9
• public class DefaultMethods implements Sayable{
• public void sayMore(String msg){
• //implementing abstract method
• System.out.println(msg); }
• public static void main(String[] args) {
• DefaultMethods dm = new DefaultMethods();
• dm.say(); // calling default method
• No Parameter Syntax:
• () -> {
• //Body of no parameter lambda
• }
07/02/2024 15
Java Lambda Expressions
• One Parameter Syntax:
• (p1) -> {
• //Body of single parameter lambda
• }
• Two Parameter Syntax:
• (p1,p2) -> {
• //Body of multiple parameter lambda
• }
07/02/2024 16
Java Lambda Expressions
• Without Lambda Expression:
• interface Drawable{
• public void draw();
• }
• public class LambdaExpressionExample {
• public static void main(String[] args) {
• int width=10;
• //without lambda, Drawable implementation using anonymous class
• Drawable d=new Drawable(){
• public void draw(){System.out.println("Drawing "+width);}
• }
• d.draw();
• }
• }
• Output: Drawing 10
07/02/2024 17
Java Lambda Expressions
• Java Lambda Expression Example:
• @FunctionalInterface //It is optional
• interface Drawable{
• public void draw();
• }
• public class LambdaExpressionExample2 {
• public static void main(String[] args) {
• int width=10;
•
• //with lambda
• Drawable d2=()->{
• System.out.println("Drawing "+width);
• };
• d2.draw();
• }
• }
07/02/2024 Output: Drawing 10 18
Java Lambda Expressions
• Java Lambda Expression Example: Single Parameter:
• interface Sayable{
• public String say(String name);
• }
• public class LambdaExpressionExample4{
• public static void main(String[] args) {
•
• // Lambda expression with single parameter.
• Sayable s1=(name)->{
• return "Hello, "+name;
• };
• System.out.println(s1.say("Sonoo"));
•
• // You can omit function parentheses
• Sayable s2= name ->{
• return "Hello, "+name;
• };
• System.out.println(s2.say("Sonoo"));
• } Output: Hello, Sonoo
• } Hello, Sonoo
07/02/2024 19
Java Lambda Expressions
• Java Lambda Expression Example: Multiple Parameter:
• interface Addable{
• int add(int a,int b);
• }
•
• public class LambdaExpressionExample5{
• public static void main(String[] args) {
•
• // Multiple parameters in lambda expression
• Addable ad1=(a,b)->(a+b);
• System.out.println(ad1.add(10,20));
•
• // Multiple parameters with data type in lambda expression
• Addable ad2=(int a,int b)->(a+b);
• System.out.println(ad2.add(100,200));
• }
• }
• Output:
• 30
• 300
07/02/2024 20
Streams
• Java provides a new additional package in Java 8 called java.util.stream.
This package consists of classes, interfaces and enum to allows functional-
style operations on the elements. You can use stream by importing
java.util.stream package.
07/02/2024 22
Streams
• public class JavaStreamExample {
• public static void main(String[] args) {
• List<Product> productsList = new ArrayList<Product>();
• //Adding Products
• productsList.add(new Product(1,"HP Laptop",25000f));
• productsList.add(new Product(2,"Dell Laptop",30000f));
• productsList.add(new Product(3,"Lenevo Laptop",28000f));
• productsList.add(new Product(4,"Sony Laptop",28000f));
• productsList.add(new Product(5,"Apple Laptop",90000f));
• List<Float> productPriceList = new ArrayList<Float>();
• for(Product product: productsList){
• // filtering data of list
• if(product.price<30000){
• productPriceList.add(product.price); // adding price to a
productPriceList
• } }
• System.out.println(productPriceList); // displaying data
07/02/2024 23
• } }
Streams
• Java Stream Example: Filtering Collection by using Stream
• import java.util.*;
• import java.util.stream.Collectors;
• class Product{
• int id;
• String name;
• float price;
• public Product(int id, String name, float price) {
• this.id = id;
• this.name = name;
• this.price = price;
• }
• }
07/02/2024 24
Streams
• Java Stream Example: Filtering Collection by using Stream
• public class JavaStreamExample {
• public static void main(String[] args) {
• List<Product> productsList = new ArrayList<Product>();
• //Adding Products
• productsList.add(new Product(1,"HP Laptop",25000f));
• productsList.add(new Product(2,"Dell Laptop",30000f));
• productsList.add(new Product(3,"Lenevo Laptop",28000f));
• productsList.add(new Product(4,"Sony Laptop",28000f));
• productsList.add(new Product(5,"Apple Laptop",90000f));
• List<Float> productPriceList2 =productsList.stream()
• .filter(p -> p.price > 30000)// filtering data
• .map(p->p.price) // fetching price
• .collect(Collectors.toList()); // collecting as list
• System.out.println(productPriceList2);
• 07/02/2024
} 25
• }
Streams
• Java Stream Iterating Example
• You can use stream to iterate any number of times. Stream provides
predefined methods to deal with the logic you implement. In the following
example, we are iterating, filtering and passed a limit to fix the iteration.
• import java.util.stream.*;
• public class JavaStreamExample {
• public static void main(String[] args){
• Stream.iterate(1, element->element+1)
• .filter(element->element%5==0)
• .limit(5)
• .forEach(System.out::println);
• }
• } Output:5
• 10 15 20 25
07/02/2024 26
MCQs
• In Java 8 Interfaces, methods can be:
• a.default
• b.abstract
• c.all
• d.none
c
07/02/2024 27
• Which is correct encoder method of
Base64 URL encoder?
a) base64.decoder
b) getUrlencoder
c) getUrldecoder
d) base64.getUrlencoder
07/02/2024
b 28
• The newly introduced Streams API is
available in which package of java 8:
• a.java.io.streams
• b.java.io.stream
• c.java.util.streams
• d.java.util.stream
07/02/2024 d 29
• how many methods are there in functional
interface in Java 8?
• a.0
• b.1
• c.2
• d.3
07/02/2024 b 30
• Stream operations in java 8 can be divided
into
• a.Terminal types
• b.Intermediate types
• c.All
• d.None
07/02/2024 c 31
• In java 8 Function is ?
• a.Class
• b.Interface
• c.Lambda Expression
• d.Object
07/02/2024 b 32
Thank you!
07/02/2024 33