title: Comparing two Strings using Java 8 features
body: <p>I was exploring various efficient ways to compare two strings in Java and I was comparing below two code snippets </p>
  
  <p><strong>Approach 1 :- Traditional Way</strong> </p>
  
  <pre><code>public static boolean stringCheck(String test1, String test2) {    
      if ((test1 == null) || (test2 == null)) {
          return false;
      }
      return test1.compareTo(test2) &gt; 0;
  }
  </code></pre>
  
  <p>and to test this code </p>
  
  <pre><code>String test1 = null;
  String test2 = null;
  boolean isTrue1 = stringCheck(test1, test2);
  System.out.println("Return of stringCheck is" + isTrue1);
  </code></pre>
  
  <p><strong><em>which prints => Return of stringCheck is false</em></strong>  which is correct </p>
  
  <p><strong>Approach 2 :- Using Java 8 Features</strong> </p>
  
  <pre><code>public static boolean stringCheckWithJava8(String test1, String test2) {
      return Optional.ofNullable(test1)
              .map(s1 -&gt; s1.equals(test2))
              .orElseGet(() -&gt; test2 == null).booleanValue();
  }
  </code></pre>
  
  <p>and to test this code </p>
  
  <pre><code>String test1 = null;
  String test2 = null;
  boolean isTrue = stringCheckWithJava8(test1, test2);
  </code></pre>
  
  <p><strong>which prints => Return of stringCheckWithJava8 is true</strong>
  which is logically correct  .</p>
  
  <p>Now my question is How can I modify my code in Approach 2 with Java 8 feature shown above to handle a to return false when both strings are null? </p>