递归地在两个列表之间生成一组所有可能的映射(Java)

递归地在两个列表之间生成一组所有可能的映射(Java),java,algorithm,dictionary,recursion,Java,Algorithm,Dictionary,Recursion,问题 我试图编写一个方法,递归地生成并返回元素之间从一个给定列表到另一个列表的一组所有可能的映射 例如,假设我输入一个整数数组列表和一个字符串数组列表 List<Integer> integers = new ArrayList<>(); integers.add(1); integers.add(2); List<String> strings = new ArrayList<>(); strings.add("a"); strings.add

问题

我试图编写一个方法,递归地生成并返回元素之间从一个给定列表到另一个列表的一组所有可能的映射

例如,假设我输入一个整数数组列表和一个字符串数组列表

List<Integer> integers = new ArrayList<>();
integers.add(1); integers.add(2);

List<String> strings = new ArrayList<>();
strings.add("a"); strings.add("b"); strings.add("c");

combinations(integers, strings);
在这里,每个字符串(值)只能映射到一个整数(键)。换句话说,不能将两个整数映射到同一个字符串

免责声明和规范

完全自由裁量权:这是家庭作业方法规范的简化版本,因此方法签名应该如下所示

Set<Map<Integer, String>> combinations(List<Integer> integers, List<String> strings);
这里的输出只是

{
 (),
 (1 -> "a", 2 -> "b")
}
这只是一个包含空映射和第一个可能映射的集合


如果有人能给我指出正确的方向,我将不胜感激

与其只提供代码,不如让我给出伪代码,让您了解如何执行。然后我建议您再试一次,如果您有任何问题,请回来:

combinations(keys, values):
    create result set
    for each key in keys
        for each value in values
            combos = combinations(keys without key, values without value)
            for each combo
                add map from key -> value to combo
                add combo to result set
    return result set
更新

既然OP已经接受了这个答案,下面是演示算法的工作代码:

Set<Map<Integer, String>> combinations(List<Integer> keys, List<String> values) {
    if (keys.isEmpty())
        return Collections.singleton(new HashMap<>());
    else
        return keys.stream().flatMap(k -> 
            values.stream().flatMap(v -> 
                combinations(listWithout(keys, k), listWithout(values, v)).stream()
                    .peek(c -> c.put(k, v)))).collect(toSet());
}

private <T> List<T> listWithout (List<T> input, T value) {
    return input.stream().filter(v -> !v.equals(value)).collect(toList());
}
设置组合(列表键、列表值){
if(key.isEmpty())
返回Collections.singleton(新的HashMap());
其他的
返回keys.stream().flatMap(k->
values.stream().flatMap(v->
组合(listWithout(键,k),listWithout(值,v)).stream()
.peek(c->c.put(k,v)).collect(toSet());
}
私有列表listWithout(列表输入,T值){
返回input.stream().filter(v->!v.equals(value)).collect(toList());
}

为什么
a-a
b-b
等没有出现?@TimBiegeleisen如果你的意思是为什么没有出现,例如,映射(1->“a”,2->“a”),那是因为我提到没有两个整数可以映射到同一个字符串。例如,如果您的意思是为什么不显示映射(“a”->“a”,“b”->“b”),那是因为它是一组从整数到字符串的映射@雅各布。谢谢我将急切地等待你的答复!:)谢谢“我会试一试,很快就回来报告的。”特里斯坦·巴克勒,很好。为了未来读者的兴趣,我将为答案添加一个解决方案。
combinations(keys, values):
    create result set
    for each key in keys
        for each value in values
            combos = combinations(keys without key, values without value)
            for each combo
                add map from key -> value to combo
                add combo to result set
    return result set
Set<Map<Integer, String>> combinations(List<Integer> keys, List<String> values) {
    if (keys.isEmpty())
        return Collections.singleton(new HashMap<>());
    else
        return keys.stream().flatMap(k -> 
            values.stream().flatMap(v -> 
                combinations(listWithout(keys, k), listWithout(values, v)).stream()
                    .peek(c -> c.put(k, v)))).collect(toSet());
}

private <T> List<T> listWithout (List<T> input, T value) {
    return input.stream().filter(v -> !v.equals(value)).collect(toList());
}