从多个Set值生成字符串排列(Java 8流) [英] Generate String Permutations from multiple Set values (Java 8 Streams)

查看:143
本文介绍了从多个Set值生成字符串排列(Java 8流)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个组合-国家和州.我想同时创建所有可能的排列.

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> stateCountryPermutations = countryPermutations.stream()
        .flatMap(country -> statePermutations.stream()
                .flatMap(state -> Stream.of(state + country, country + state)))
        .collect(Collectors.toSet());

    Set<String> finalAliases = Optional.ofNullable(stateCountryPermutations)
            .map(Collection::stream).orElse(Stream.empty())
                .map(sc -> "houston " + sc)
                .collect(Collectors.toSet());
    System.out.println(stateCountryPermutationAliases);
 }
}

州或国家或两个排列都可以为null.我仍然希望我的代码起作用.

The state or country or both permutations can be null. I still want my code to function.

要求

  1. 如果状态排列为null,则最终输出应为[休斯敦美国,休斯敦美国]

  1. If state permutation is null, final output should be [Houston USA, Houston United States of America]

如果国家/地区排列为空,则最终输出应为[休斯敦德克萨斯州休斯顿]

If country permutations is null, final output should be [Houston TX, Houston Texas]

如果两者均为空,则无输出

If both are null, then no output

我将代码更改为以下

Set<String> stateCountryPermutations = 
   Optional.ofNullable(countryPermutations)
           .map(Collection::stream)
           .orElse(Stream.empty())
           .flatMap(country -> Optional.ofNullable(statePermutations)
                                       .map(Collection::stream)
                                       .orElse(Stream.empty())
                                       .flatMap(state -> Stream.of(state + country, country + state)))
           .collect(Collectors.toSet());

这满足3.当任一排列为空时,1& 2个不满意.我没有别名作为回应.如何修改我的代码?

This satisfies 3. When either permutation is null, 1 & 2 are not satisfied. I get no aliases as response. How do I modify my code?

推荐答案

以下代码从任意数量的输入集创建所有组合,而忽略空/空集:

The following code creates all combinations from any number of input sets, ignoring null/empty sets:

Stream<Collection<String>> inputs = Stream.of(Arrays.asList("United States of america", "USA"),
                                              Arrays.asList("Texas", "TX"), 
                                              Arrays.asList("Hello", "World"),
                                              null,
                                              new ArrayList<>());

Stream<Collection<List<String>>> listified = inputs.filter(Objects::nonNull)
                                                   .filter(input -> !input.isEmpty())
                                                   .map(l -> l.stream()
                                                              .map(o -> new ArrayList<>(Arrays.asList(o)))
                                                              .collect(Collectors.toList()));

Collection<List<String>> combinations = listified.reduce((input1, input2) -> {
    Collection<List<String>> merged = new ArrayList<>();
    input1.forEach(permutation1 -> input2.forEach(permutation2 -> {
        List<String> combination = new ArrayList<>();
        combination.addAll(permutation1);
        combination.addAll(permutation2);
        merged.add(combination);
    }));
    return merged;
}).orElse(new HashSet<>());

combinations.forEach(System.out::println);

输出:

[United States of america, Texas, Hello]
[United States of america, Texas, World]
[United States of america, TX, Hello]
[United States of america, TX, World]
[USA, Texas, Hello]
[USA, Texas, World]
[USA, TX, Hello]
[USA, TX, World]

现在,您可以使用您提到的帮助方法来创建每个组合的排列. 此问题显示了如何生成列表的所有排列.

Now you can use your mentioned helper method to create the permutations of each combination. This question shows how to generate all permutations of a list.

这篇关于从多个Set值生成字符串排列(Java 8流)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆