title: Generate String Permutations from multiple Set values (Java 8 Streams)
body: <p>I have two Sets - country and state. I want to create all possible permutations from both.</p>
  <pre class="lang-java prettyprint-override"><code>import java.util.*;
  import java.util.stream.Collectors;
  
  public class HelloWorld {
    public static void main(String[] args) {
      System.out.println(&quot;Hello World&quot;);
  
      Set&lt;String&gt; countryPermutations = new HashSet&lt;&gt;(
          Arrays.asList(&quot;United States of america&quot;, &quot;USA&quot;));
      Set&lt;String&gt; statePermutations = new HashSet&lt;&gt;(
          Arrays.asList(&quot;Texas&quot;, &quot;TX&quot;));
  
      Set&lt;String&gt; stateCountryPermutations = countryPermutations.stream()
          .flatMap(country -&gt; statePermutations.stream()
              .flatMap(state -&gt; Stream.of(state + country, country + state)))
          .collect(Collectors.toSet());
  
      Set&lt;String&gt; finalAliases = Optional.ofNullable(stateCountryPermutations)
          .map(Collection::stream).orElse(Stream.empty())
          .map(sc -&gt; &quot;houston &quot; + sc)
          .collect(Collectors.toSet());
      System.out.println(stateCountryPermutationAliases);
    }
  }
  </code></pre>
  <p>The state or country or both permutations can be null. I still want my code to function.</p>
  <p>Requirements</p>
  <ol>
  <li><p>If state permutation is null, final output should be [Houston USA, Houston United States of America]</p>
  </li>
  <li><p>If country permutations is null, final output should be [Houston TX, Houston Texas]</p>
  </li>
  <li><p>If both are null, then no output</p>
  </li>
  </ol>
  <p>I'm changed my code to below</p>
  <pre class="lang-java prettyprint-override"><code>Set&lt;String&gt; stateCountryPermutations =
      Optional.ofNullable(countryPermutations)
          .map(Collection::stream)
          .orElse(Stream.empty())
          .flatMap(country -&gt; Optional.ofNullable(statePermutations)
              .map(Collection::stream)
              .orElse(Stream.empty())
              .flatMap(state -&gt; Stream.of(state + country, country + state)))
          .collect(Collectors.toSet());
  </code></pre>
  <p>This satisfies 3. When either permutation is null, 1 &amp; 2 are not satisfied. I get no aliases as response. How do I modify my code?</p>