3

I have two Sets - country and state. I want to create all possible permutations from both.

import java.util.*;
import java.util.stream.Collectors;

public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");

        Set<String> countryPermutations = new HashSet<>(Arrays.asList("United States of america", "USA"));

        Set<String> statePermutations = new HashSet<>(Arrays.asList("Texas", "TX"));

        Set<String> stateCountryPermutationAliases = countryPermutations.stream()
        .flatMap(country -> statePermutations.stream()
        .map(state -> state + country))
        .collect(Collectors.toSet());

        System.out.println(stateCountryPermutationAliases);
     }
    }

This gives the output

[TexasUSA, TXUSA, TXUnited States of america, TexasUnited States of america]

I however want the opposite concatenation as well - country + state. How can I extend my lambda to do this?

1 Answer 1

4
 Set<String> stateCountryPermutationAliases = countryPermutations.stream()
            .flatMap(country -> statePermutations.stream()
                    .flatMap(state -> Stream.of(state + country, country + state)))
            .collect(Collectors.toSet());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.