Saturday, 12 May 2018

                                                           Optional class -jdk 8 concept


package java8;

public class OptionalClassDemo {
            public static void main(String[] args) {
                        String []s1=new String[5];
                        String s2=s1[3].toLowerCase();
                        System.out.println(s2);
                       
            }

}

o/p:----------
Exception in thread "main" java.lang.NullPointerException
       at java8.OptionalClassDemo.main(OptionalClassDemo.java:6)
package java8;

import java.util.Optional;

public class OptionalClassDemo1 {
            public static void main(String[] args) {
                        String []s1=new String[5];
                        Optional<String> kk = Optional.ofNullable(s1[3]);
                        if(kk.isPresent())
                        {
                                    String s2 = s1[3].toLowerCase();
                                    System.out.println(s2);
                        }
                       
                        else
                        {
                                    System.out.println("String value is not present");
                        }
                                               
            }

}

o/p:---------------

String value is not present
========================================================================================




package java8;

import java.util.Optional;

public class OptionalClassDemo3 {
            public static void main(String[] args) {
                        String s1[]=new String[5];
                        s1[3]="govind ballabh khan";
                        Optional<String> kk = Optional.ofNullable(s1[3]);
                        if(kk.isPresent())
                        {
                        String s2 = s1[3].toLowerCase();
                        System.out.println(s2);
                        }
                        else
                        {
                                    System.out.println("String value is not possible");
                        }
                       
            }

}


o/p:-------------------

govind ballabh khan


package java8;

import java.util.Optional;

public class OptionalClassDemo4 {
            public static void main(String[] args) {
                        String [] s1=new String[5];
                        s1[3]="Govind Ballabh Khan";
                        Optional<String> kk = Optional.ofNullable(s1[3]);
                        kk.ifPresent(System.out::println);// printing value by using method reference
                        System.out.println(kk.get());// printing value by using get method
                        System.out.println(s1[3].toLowerCase());
            }}
o/p:-----------------
Govind Ballabh Khan
Govind Ballabh Khan
govind ballabh khan
==========================================================================================


package java8;

import java.util.Optional;
public class OptionClassDemo5 {
            public static void main(String[] args) {
                        String s1[]=new String[5];
                        s1[3]="Govind Ballabh Khan";
                        Optional<Object> empty = Optional.empty();
                        System.out.println(empty);
                        Optional<String> op = Optional.of(s1[3]);
                        System.out.println(op);
                        System.out.println("Filtered Value="+op.filter((p)->p.equals("Mnc")));
                        System.out.println("Filtered Value="+op.filter((p)->p.equals("Govind Ballabh Khan")));
                        System.out.println("Getting values:"+op.get());
                        System.out.println("Getting HashCode:"+op.hashCode());
                        System.out.println("Is Value Present:"+op.isPresent());
                        System.out.println("Nullable Optional:"+op.ofNullable(s1[3]));
                        System.out.println("orElse:"+op.orElse("value is not present"));
                        System.out.println("orElse:"+empty.orElse("Value is not present"));
                        op.ifPresent((System.out::println));
                       
                       
                       
                       
                       
            }

}


o/p:------------------------

Optional.empty
Optional[Govind Ballabh Khan]
Filtered Value=Optional.empty
Filtered Value=Optional[Govind Ballabh Khan]
Getting values:Govind Ballabh Khan
Getting HashCode:-233837391
Is Value Present:true
Nullable Optional:Optional[Govind Ballabh Khan]
orElse:Govind Ballabh Khan
orElse:Value is not present
Govind Ballabh Khan

=======================================================================================









No comments:

Post a Comment