Java 8

 

  1. we identified two problems.
    1. Data Missing
    2. Instead o dealing with the problem,  we r escaping from the problem by just putting !=null
  2. We came up with two solutions
    1. Returning the Optional<Dept> in the getter method (best Case)
    2. Handle while processing the stream (alternative solution) by providing some default values.
  3. When and Where to Declare Optional ?
    1. Method Return Type
  4. When to not use Optional<> ?
    1. instance Variables of value object. Reason: During the Serialization it will throw the exception. sample output as follows

 

image

                  b. writing Optional to JSON produces undesired results

                  c. Avoid using Optional as a method parameter

         5. How we return a default value ?

                    using orElse() or orElseGet()  as we see in the code

Comments