地图<对象,列表<对象>的组合> [英] Combinations of Map<Object, List<Object>>
问题描述
我有一个 HashMap< GC,List< RR>>
,其中包含样本数据:
键值
gc1 - rr1
- rr2
- rr3
gc2 - rr4
- rr5
gc3 - rr6
我需要创建所有可能的RR组合来自不同的GC:
组合1:rr1,rr4,rr6
组合2:rr1,rr5,rr6
组合3:rr2,rr4,rr6
组合4:rr2,rr5,rr6
组合5:rr3,rr4,rr6
组合6:rr3,rr5,rr6
到目前为止我尝试过的是,正如@Sanket Makani所建议的,将我的 HashMap< GC,List< RR>>< / code>转换为
List< List< RR>>
,然后遍历所有元素:
列表<列表< RR>> inputList = new ArrayList< List< RR>>(); (Map.Entry< GC,List< RR> rrList:Map.entrySet()){
inputList.add(rrList.getValue());
。
}
列表<列表< RR>> combinationList = new ArrayList< List< RR>>(); (列表< RR> rrList:inputList){
List< RR>
。 rrList1 = new ArrayList< RR>(); (RR rr:rrList){
rrList1.add(rr);
}
combinationsList.add(rrList1);
}
这不适用于我,因为它将一个GC内的所有RR例如:
组合1:rr1,rr2,rr3
组合2:rr4,rr5
组合3:rr6
所以我的问题是,我如何调整我的代码以获得预期的结果?
PS:我正在使用Java6,因此不允许使用lambda / streams。
PS2:我见过类似的问题,但找不到和我正在寻找的确切例子。
编辑:
是@ nandsito的答案的最终实现:
//此方法通过GC key与给定列表分组RR
HashMap< GC,List< RR>> GCRRHashMap = groupRRsByGC(list);
列表< Map.Entry< GC,List< RR>>> mapEntryList = new ArrayList< Map.Entry< GC,List< RR>>>(GCRRHashMap.entrySet());
列表<列表< RR>> combinationList = new ArrayList< List< RR>>();
列表< RR>组合=新的ArrayList< RR>();
generateCombinations(mapEntryList,combinations,combinationsList);
private void generateCombinations(
List< Map.Entry< GC,List< RR>>> mapEntryList,
List< RR> (新的ArrayList< RoomStay>(组合))的组合列表(列表< RR>组合列表){
if(mapEntryList.isEmpty()){
combinationsList.add
return;
}
Map.Entry< GC,List< RR>> entry = mapEntryList.remove(0);
列表< RR> entryValue = new ArrayList< RR>(entry.getValue()); $!
$ b while(!entryValue.isEmpty()){
RR rr = entryValue.remove(0);
combinations.add(rr);
generateCombinations(mapEntryList,combinations,combinationsList);
combinations.remove(combinations.size() - 1);
}
mapEntryList.add(0,entry);
$ / code $ / pre
解决方案递归解决方案:
$ b
public static void main(String [] args){
//您的数据地图
地图< GC,列表< RR>>地图;
//将地图条目设置为列表,这将有助于
//组合元素
//
//注意这是一个可修改的列表
列表< Map.Entry< GC,List< RR>>> mapEntryList =
new ArrayList< Map.Entry< GC,List< RR>>>(map.entrySet());
//组合列表,它将存储
//所需结果
列表< RR>组合=新的ArrayList< RR>();
doRecursion(mapEntryList,组合);
}
private static void doRecursion(
List< Map.Entry< GC,List< RR>> mapEntryList,
List< RR> {
//递归结束
if(mapEntryList.isEmpty()){
//做你想做的事
/ /
//这里我打印组合
的每个元素(RR rr:组合){
System.out.println(rr);
}
System.out.println();
return;
}
//从条目列表中删除一个GC
//然后对于每个来自GC
的RR,//将RR放入组合列表中,
//调用递归
//从组合列表中删除RR
//结束每个
//然后将GC放回其列表中
Map.Entry< GC,List< RR>> entry = mapEntryList.remove(0);
列表< RR> entryValue = new ArrayList< RR>(entry.getValue()); $!
$ b while(!entryValue.isEmpty()){
RR rr = entryValue.remove(0);
combinations.add(rr);
doRecursion(mapEntryList,组合);
combinations.remove(combinations.size() - 1);
}
mapEntryList.add(0,entry);
}
I have a HashMap<GC, List<RR>>
with sample data like:
key values
gc1 - rr1
- rr2
- rr3
gc2 - rr4
- rr5
gc3 - rr6
And I need to create all possible combinations of RR from different GC like:
Combination1: rr1, rr4, rr6
Combination2: rr1, rr5, rr6
Combination3: rr2, rr4, rr6
Combination4: rr2, rr5, rr6
Combination5: rr3, rr4, rr6
Combination6: rr3, rr5, rr6
What I've tried so far is, as @Sanket Makani suggests, to turn my HashMap<GC, List<RR>>
into a List<List<RR>>
, and then iterate through all the elements like:
List<List<RR>> inputList = new ArrayList<List<RR>>();
for (Map.Entry<GC, List<RR>> rrList : Map.entrySet()) {
inputList.add(rrList.getValue());
}
List<List<RR>> combinationsList = new ArrayList<List<RR>>();
for (List<RR> rrList : inputList) {
List<RR> rrList1 = new ArrayList<RR>();
for (RR rr : rrList) {
rrList1.add(rr);
}
combinationsList.add(rrList1);
}
This is not working for me, as it groups all the RR inside one GC like:
Combination1: rr1, rr2, rr3
Combination2: rr4, rr5
Combination3: rr6
So my quesiton is, how can I adapt my code to obtain the expected result?
PS: I'm working with Java6 unfortunately, so no lambdas/streams allowed.
PS2: I've seen similar questions, but can't find and exact example of what I'm looking for.
EDIT:
This is my final implementation with @nandsito's answer:
//this method groups RRs by GC key with a given list
HashMap<GC, List<RR>> GCRRHashMap = groupRRsByGC(list);
List<Map.Entry<GC, List<RR>>> mapEntryList = new ArrayList<Map.Entry<GC, List<RR>>>(GCRRHashMap.entrySet());
List<List<RR>> combinationsList = new ArrayList<List<RR>>();
List<RR> combinations = new ArrayList<RR>();
generateCombinations(mapEntryList, combinations, combinationsList);
private void generateCombinations(
List<Map.Entry<GC, List<RR>>> mapEntryList,
List<RR> combinations, List<List<RR>> combinationsList) {
if (mapEntryList.isEmpty()) {
combinationsList.add(new ArrayList<RoomStay>(combinations));
return;
}
Map.Entry<GC, List<RR>> entry = mapEntryList.remove(0);
List<RR> entryValue = new ArrayList<RR>(entry.getValue());
while (!entryValue.isEmpty()) {
RR rr = entryValue.remove(0);
combinations.add(rr);
generateCombinations(mapEntryList, combinations, combinationsList);
combinations.remove(combinations.size() - 1);
}
mapEntryList.add(0, entry);
}
解决方案 Here's a recursive solution:
public static void main(String[] args) {
// your data map
Map<GC, List<RR>> map;
// the map entry set as list, which will help
// combining the elements
//
// note this is a modifiable list
List<Map.Entry<GC, List<RR>>> mapEntryList =
new ArrayList<Map.Entry<GC, List<RR>>>(map.entrySet());
// the combinations list, which will store
// the desired results
List<RR> combinations = new ArrayList<RR>();
doRecursion(mapEntryList, combinations);
}
private static void doRecursion(
List<Map.Entry<GC, List<RR>>> mapEntryList,
List<RR> combinations) {
// end of recursion
if (mapEntryList.isEmpty()) {
// do what you wish
//
// here i print each element of the combination
for (RR rr : combinations) {
System.out.println(rr);
}
System.out.println();
return;
}
// remove one GC from the entry list,
// then for each RR from the taken GC
// put RR in the combinations list,
// call recursion
// the remove RR from the combinations list
// end for each
// then put GC back into its list
Map.Entry<GC, List<RR>> entry = mapEntryList.remove(0);
List<RR> entryValue = new ArrayList<RR>(entry.getValue());
while (!entryValue.isEmpty()) {
RR rr = entryValue.remove(0);
combinations.add(rr);
doRecursion(mapEntryList, combinations);
combinations.remove(combinations.size() - 1);
}
mapEntryList.add(0, entry);
}
这篇关于地图<对象,列表<对象>的组合>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!