Skip to main content

Schema কি? (What is schema?)

Java 9 Optional Class Improvements

Optional class is introduced in Java 8 to prevent the null checks and NullPointerException. Before Java 8, if-constructs are used to check the null values. But, it is not an perfect way to check for null value as it doesn’t resolve NullPointerException although it just hides it and propagates it to the next level. Therefore, inspired by other functional programming languages, Optional class is introduced in Java to handle the null values from Java 8.

Java 9 Optional Class Improvements :


of()empty()ofNullable()get()ifPresent()isPresent()orElse()orElseGet()orElseThrow()map()flatMap() and filter() are the methods of Java 8 Optional class. Three more methods are added to Optional class from Java 9. 

three new methods are added to improve its functionality.

  • stream()
  • ifPresentOrElse()
  • or()

 Let’s see these methods in detail.



ifPresentOrElse() Method :

This method performs the given action if the value is present in the Optional object. If the value is absent, performs the given empty-based action.

You can use this method when you want to perform different actions depending upon the presence and absence of a value.














  1. import java.util.Optional;

  2. public class Java9OptionalImprovements
  3. {
  4.     public static void main(String[] args)
  5.    {
  6.         //Optional object with a value
  7.          
  8.         Optional<String> optionalAddress_1 = Optional.of("Address_1");
  9.          
  10.         optionalAddress_1.ifPresentOrElse(address -> System.out.println("Address : "+address), () -> System.out.println("No Address"));
  11.          
  12.         //Optional object without a value
  13.          
  14.         Optional<String> optionalAddress_2 = Optional.empty();
  15.          
  16.         optionalAddress_2.ifPresentOrElse(address -> System.out.println("Address : "+address), () -> System.out.println("No Address"));
  17.     }
  18. }

Output :

Address : Address_1
No Address

or() Method :

This method returns an Optional object containing the value if the value is present in the given Optional object. If the value is not present, it returns an Optional produced by the supplying function.

This method is similar to orElse() and orElseGet() which return unwrapped value where as this method returns the value wrapped in another Optional.
























  1. import java.util.Optional;
  2.  
  3. public class Java9OptionalImprovements
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         //Optional object with a value
  8.          
  9.         Optional<String> optionalAddress_1 = Optional.of("Address_1");
  10.          
  11.         Optional<String> optional = optionalAddress_1.or(() -> Optional.of("No Address"));
  12.          
  13.         System.out.println(optional.get());
  14.          
  15.         //Optional object without a value
  16.          
  17.         Optional<String> optionalAddress_2 = Optional.empty();
  18.          
  19.         optional = optionalAddress_2.or(() -> Optional.of("No Address"));
  20.          
  21.         System.out.println(optional.get());
  22.     }
  23. }

Output :

Address_1
No Address

stream() Method :

This method returns a stream containing the value if the value is present in the given Optional object. If the value is not present, it returns an empty stream.

This method converts Optional into Stream and enables the developers to use all operations of Stream API with an Optional object also.



















  1. import java.util.Optional;
  2.  
  3. public class Java9OptionalImprovements
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         //Optional object with a value
  8.          
  9.         Optional<String> optionalAddress_1 = Optional.of("Address_1");
  10.          
  11.         optionalAddress_1.stream().forEach(System.out::println);
  12.          
  13.         //Optional object without a value
  14.          
  15.         Optional<String> optionalAddress_2 = Optional.empty();
  16.          
  17.         optionalAddress_2.stream().forEach(System.out::println);
  18.     }
  19. }

Output :

Address_1


 


Comments

Post a Comment

Popular posts from this blog

SEO Questions and Answers

  Zahins SEO Questions and Answers  This website is all about training.  Zahins SEO Questions and Answers will help to prepare for SEO Exam. These are the common question and answer for SEO exam. I recommend doing some extra research before taking any exam. To success you need to get clear concept of everything. specially get a very good idea about all SEO sectors like On page Off page etc.    1.    What do you use to search for exact phrases in Google? A.    Hyphen (-) B.    Asterisk (*) C.    Quotation Marks (“) D.   Exclamation Point (!) Answer:  Option C   2.    Are RSS/Atom feeds returned in Google’s search results? A.    Yes B.    No Answer: Option B   3.    An HTML sitemap provides a list of internal links on a website accessible to users. A.    True B.    False Answer: Option A   4.    What is Anchor Text? A.    It is the main body of text on a particular web page B.    It is the text within the left or top panel of a web page C.    It is

Schema কি? (What is schema?)

Schema প্রতিষ্ঠিত হয়েছে Google, Microsoft, Yahoo and Yandex, Schema.org এর developer দের একটি গ্রুপের সমন্বয়য়ে। আর এটা হয়েছে GitHub এবং https://lists.w3.org/Archives/Public/public-schemaorg/ এর সমন্বয়ে। Internet, web pages, email messages সহ এবং এর বাইরে structured data কে promote করার জন্য।  তিন ধরনের encodings ব্যাবহার করে Schema লিখা যেতে পারে।  যেমনঃ ১। RDFa             ২। Microdata এবং             ৩। JSON-LD এই তিন টির মধ্যে JSON-LD Google Recommend করে।  যে সাইট টি দিয়ে আমরা এই code সম্পর্কে জানি তা হল Schema.org এছাড়াও আরও সাইট আছে যা দিয়ে এই code generate করা সম্ভব তার মধ্যে অন্যতম https://schemagenerator.net/ এছাড়াও Google Markup tools দিয়েও করা যেতে পারে।

How to fix search console coverage error for Cart, My Account, and Checkout

  Woocommerce adds a noindex tags to certain pages. For that we receive some error on search console. Yoast try add all page for indexing where woocommerce adds noindex that's the conflict we need to resolve. One way to remove this is not set those file as index but I don't recommended because we should not index cart, my account, checkout etc. better approach is to add some code to function.php The following code snippet should help remove Cart, My Account, and Checkout  for you: if ( ! function_exists( 'wd_remove_woo_pages_from_yoast_sitemap' ) ) { /** * If Woocommerce and Yoast are enabled, we need to prevent Yoast from showing My Account, Checkout, and Cart in the sitemap * * @param $exclusions_array * @author Nick Jeffers @ Websites Depot Inc * @since * @return array */ function wd_remove_woo_pages_from_yoast_sitemap( $exclusions_array ) { // make sure Woocommerce is enabled if ( function_exists( 'wooc