Map元素的Java组合<;A、 列表<;B>&燃气轮机;
(注:我只是重写了这个问题,因为我以为它是在处理排列,但实际上它是在处理组合。)Map元素的Java组合<;A、 列表<;B>&燃气轮机;,java,algorithm,map,combinations,Java,Algorithm,Map,Combinations,(注:我只是重写了这个问题,因为我以为它是在处理排列,但实际上它是在处理组合。) 更具体地考虑一个Map好的,下面是我自己的尝试:尽管如此,我仍然需要对它进行测试,并且在以后的日期之前无法进行测试: 映射单词组和分数=0;索引--){ if(index[index]
更具体地考虑一个
Map好的,下面是我自己的尝试:尽管如此,我仍然需要对它进行测试,并且在以后的日期之前无法进行测试:
映射单词组和分数代码>=0;索引--){
if(index[index]
这将给出地图中所有可能的索引组合,并逐一进行处理
编辑:更新了处理函数以显示如何检索元素
编辑2:这个答案是错误的。确实生成了一些组合,但肯定不是全部
编辑3:答案现在是正确的,经过测试并正常工作。使用递归函数的伪代码示例。每个递归级别处理一个列表,方法是逐个获取所有元素,将它们放在输出变量中,然后递归地调用自己来处理下一个迭代级别
void allCombinations(Map<A, List<B>> input, Map<A, B> output){
if (input not empty){
(x, Y) = input.removeOneElement(); //removes one list from the input
for each b in Y{
output.insert(x, b); //adds the element to the output
allCombinations(input, output); //recursively calls itself
output.remove(x, b); //removes the element from the output
}
}else{
print(output) //here i print the output
}
}
void所有组合(映射输入、映射输出){
如果(输入不为空){
(x,Y)=input.removeOneElement();//从输入中删除一个列表
对于Y中的每个b{
output.insert(x,b);//将元素添加到输出中
所有组合(输入、输出);//递归地调用自身
output.remove(x,b);//从输出中删除元素
}
}否则{
print(output)//这里我打印输出
}
}
因此,这可以通过使用递归有效地创建sizeof(输入)嵌套循环
您可以使用以下命令来调用它:
allCombinations(input, new Map<A, B>());
List<Map<A,B>> result = new List<Map<A,B>>();
allCombinations(input, new Map<A, B>(), result);
所有组合(输入,新映射());
注意:如果不是打印输出,而是要返回输出。然后更改方法的签名:
void allCombinations(Map<A, List<B>> input, Map<A, B> output, List<Map<A,B>> result)
...
result.add(output); //instead of print(output);
void所有组合(映射输入、映射输出、列表结果)
...
结果。添加(输出)//代替打印(输出);
并使用以下命令调用它:
allCombinations(input, new Map<A, B>());
List<Map<A,B>> result = new List<Map<A,B>>();
allCombinations(input, new Map<A, B>(), result);
列表结果=新列表();
所有组合(输入、新映射()、结果);
下面的Clojure代码以一种健壮、快速、实用的方式解决了您的需求:
(定义组合*[acc对]
(如果让[[我的密钥我的VAL](第一对)]
(地图猫)
(fn[我的val]
(组合)*
(适用于[m acc](关联我的钥匙我的val))
(其余两对)
我的VAL)
行政协调会)
(定义组合[地图]
(组合*[{}](向量映射)))
上面的代码是一个递归解决方案。它在普通英语中的作用如下。
combinations*
是一个函数,它给出一个可能的底图列表和一个键对多值对列表,返回将键值关联到输入底图的所有可能组合。这是以递归的方式完成的。如果键到多个值对的列表为空,那么我们将不会将任何内容与基本映射相关联,而是返回未修改的基映射。否则,如果有任何对,那么我们将第一个键取为多值对,对于其中的所有值,以及作为输入给定的所有底图,我们将创建所有组合,以便将这些键值添加到底图中。此修改后的基本映射组合列表将用作递归调用组合*
的新基本映射列表,剩余的键对多个值对作为第二个参数。我们进行这种组合和修改基本映射的递归,直到用完键到多个值对为止。在这一点上,如上所述,我们返回未修改的基映射作为解决方案,并将它们与来自递归的其他分支的解决方案连接在一起。为了初始化函数以解决我们的问题,我们必须使用空映射的单例列表作为基本映射,这是在combinations
函数中完成的。它唯一的参数是一个multi-map,它将其拆分为一个键对多值对向量,用它调用组合*
这就是它的名称:
(组合{“发票编号”[“0001”“0002”]
“发票日期”[“2013-10-07”]
“不含增值税的价格”[10.00M]
“高增值税”[2.10M]
“价格含增值税”[12.10M 14.10M]})
这是输出:
({“发票日期”2013-10-07),
发票号码“0001”,
“高增值税”2.10米,
“价格不含增值税”1000万美元,
“价格增值税”12.10M}
{“发票日期”2013-10-07,
发票编号“0002”,
“高增值税”2.10米,
“价格不含增值税”1000万美元,
“价格增值税”12.10M}
{“发票日期”2013-10-07,
发票号码“0001”,
“高增值税”2.10米,
“价格不含增值税”1000万美元,
“价格增值税”14.10M}
{“发票日期”2013-10-07,
发票编号“0002”,
“高增值税”2.10米,
“价格不含增值税”1000万美元,
“价格增值税”14.10M})
尝试将其转换为Java,或者只包含Clojure依赖项,添加Java类生成指令,并直接从Java代码调用它,如所述。您还可以测试上述代码,而无需费心在本地设置Clojure环境
更新
为了便于讨论和理解,我将很快添加一个Java版本
更新2
好了
List<Map<A,B>> result = new List<Map<A,B>>();
allCombinations(input, new Map<A, B>(), result);
private static List<HashMap<String, Object>> associateInAll(
List<HashMap<String, Object>> orig, String key, Object val) {
LinkedList<HashMap<String, Object>> result =
new LinkedList<HashMap<String, Object>>();
for (HashMap<String, Object> m : orig) {
HashMap<String, Object> mCopy = new HashMap<String, Object>(m);
mCopy.put(key, val);
result.add(mCopy);
}
return result;
}
private static List<HashMap<String, Object>> combinations2(
List<HashMap<String, Object>> acc,
List<Entry<String, List<Object>>> pairs) {
if (!pairs.isEmpty()) {
Entry<String, List<Object>> first = pairs.get(0);
String myKey = first.getKey();
List<Object> myVals = first.getValue();
LinkedList<Entry<String, List<Object>>> rest =
new LinkedList<Entry<String, List<Object>>>(pairs);
rest.removeFirst();
LinkedList<HashMap<String, Object>> results =
new LinkedList<HashMap<String, Object>>();
for (Object myVal : myVals) {
List<HashMap<String, Object>> newBaseMaps =
associateInAll(acc, myKey, myVal);
List<HashMap<String, Object>> subcombinations =
combinations2(newBaseMaps, rest);
results.addAll(subcombinations);
}
return results;
}
return acc;
}
private static List<HashMap<String, Object>> combinations(
HashMap<String, List<Object>> map) {
LinkedList<HashMap<String, Object>> baseMaps =
new LinkedList<HashMap<String, Object>>();
baseMaps.add(new HashMap<String, Object>());
LinkedList<Entry<String, List<Object>>> pairs =
new LinkedList<Entry<String, List<Object>>>(map.entrySet());
return combinations2(baseMaps, pairs);
}
public static void main(String... args) {
HashMap<String, List<Object>> input =
new HashMap<String, List<Object>>();
input.put("invoiceNumber",
Arrays.<Object>asList("0001", "0002", "0003"));
input.put("invoiceDate",
Arrays.<Object>asList("2013-10-07"));
input.put("priceExclVAT",
Arrays.<Object> asList(new BigDecimal("10.00")));
input.put("highVAT",
Arrays.<Object>asList(new BigDecimal("2.10")));
input.put("priceInclVAT",
Arrays.<Object>asList(new BigDecimal("12.10"), new BigDecimal("14.10")));
List<HashMap<String, Object>> results = combinations(input);
for (HashMap<String, Object> combination : results) {
System.out.println("=============================");
for (Entry<String, Object> entry : combination.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
00000
10000
20000
01000
11000
21000
02000
...
// some hard-coded data
static String[][] strArr = {{"invoiceNumber", "0001", "0002"},
{"invoiceDate", "2013-10-07"},
{"priceExclVAT", "10.00"},
{"highVAT", "2.10"},
{"priceInclVAT", "12.10", "14.10"}};
static int[] indices = new int[strArr.length];
static boolean increment(int index)
{
// when we can simply increase the current element
if (indices[index] < strArr[index].length-2)
{
indices[index]++;
return true;
}
// when we need to reset this element to 0 and increase the next element
else
{
if (index == strArr.length-1)
// we reached the end of the last list, so we're done
return false;
indices[index] = 0;
return increment(index+1);
}
}
static void print()
{
System.out.println(Arrays.toString(indices));
for (int i = 0; i < strArr.length; i++)
System.out.println(strArr[i][0] + ", " + strArr[i][indices[i]+1]);
System.out.println();
}
public static void main(String[] args)
{
// simply repeatedly print the output, then increment
do
{
print();
}
while (increment(0));
}