Lambda 使用Java流从两个集合创建置换
我有两套——国家和州。我想从两者中创建所有可能的排列Lambda 使用Java流从两个集合创建置换,lambda,java-stream,Lambda,Java Stream,我有两套——国家和州。我想从两者中创建所有可能的排列 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
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);
}
}
然而,我也想要相反的连接——国家+州。如何扩展lambda以实现此目的?Set stateCountryPermutationAlias=countryPermutations.stream()
Set<String> stateCountryPermutationAliases = countryPermutations.stream()
.flatMap(country -> statePermutations.stream()
.flatMap(state -> Stream.of(state + country, country + state)))
.collect(Collectors.toSet());
.flatMap(国家->状态置换.stream()
.flatMap(state->Stream.of(state+country,country+state)))
.collect(收集器.toSet());
Set<String> stateCountryPermutationAliases = countryPermutations.stream()
.flatMap(country -> statePermutations.stream()
.flatMap(state -> Stream.of(state + country, country + state)))
.collect(Collectors.toSet());