# Logical Help needed (All possible combinations)

Kunal Sathe

Ranch Hand

Posts: 49

posted 11 years ago

Hello,

I am trying to write a java method which takes a hashmap as the input. The hashmap looks as follows:

Key Value

**** ******

Q1 V11,V12,V13,V14....V1n (This is a collection)

Q2 V21,V22,V23,V24....V2n (This is a collection)

.

.

.

.

Qm Vm1,Vm2,Vm3,Vm4....Vmn (This is a collection)

The method should process the hashmap and make all possible combinations as follows:

Combination1: V11, V21, V31.....Vm1

Combination2: V11, V21, V32, V41.......Vm1

and so on.

So basically I am trying to make all possible combinations of the values.

The method will return a collection of these combinations.

I will really appreciate if anybody could shed some light on the logic. I am totally boggled at the moment.

Thanks,

Kunal

I am trying to write a java method which takes a hashmap as the input. The hashmap looks as follows:

Key Value

**** ******

Q1 V11,V12,V13,V14....V1n (This is a collection)

Q2 V21,V22,V23,V24....V2n (This is a collection)

.

.

.

.

Qm Vm1,Vm2,Vm3,Vm4....Vmn (This is a collection)

The method should process the hashmap and make all possible combinations as follows:

Combination1: V11, V21, V31.....Vm1

Combination2: V11, V21, V32, V41.......Vm1

and so on.

So basically I am trying to make all possible combinations of the values.

The method will return a collection of these combinations.

I will really appreciate if anybody could shed some light on the logic. I am totally boggled at the moment.

Thanks,

Kunal

Chris Johnston

Ranch Hand

Posts: 85

posted 11 years ago

I am not totally sure what it is you want here, but what ever it is, it sounds really expensive with an operation that will probably border on O(n^2) complexity. The larger your hashmap and the larger the collections contained within that hashmap, the longer it is going to take to process the probabilities.

Can you be a little more clear as to what you are trying to do?

Can you be a little more clear as to what you are trying to do?

Kunal Sathe

Ranch Hand

Posts: 49

posted 11 years ago

Stefan and Chris,

Sorry about the confusion.

For x=2 and n=3 the inputted HashMap would look like this

Key Value

*** *****

Q1 V11 V12 V13

Q2 V21 V22 V23

Now all possible combinations are:

Combination 1: V11, V21

Combination 2: V11, V22

Combination 3: V11, V23

Combination 4: V12, V21

Combination 5: V12, V22

Combination 6: V12, V23

Combination 7: V13, V21

Combination 8: V13, V22

Combination 9: V13, V23

No combination can ever be repeated.

The number of elements in a valid combination should always be equal to x=2.

Now, the hashmap may also look like this:

Key Value

*** *****

Q1 V11 V12

Q2 V21 V22 V23

Here the value of n is not the same for each collection. This can also be one case.

The elements in the collection may or may not be sorted.

Thanks,

Kunal

Sorry about the confusion.

For x=2 and n=3 the inputted HashMap would look like this

Key Value

*** *****

Q1 V11 V12 V13

Q2 V21 V22 V23

Now all possible combinations are:

Combination 1: V11, V21

Combination 2: V11, V22

Combination 3: V11, V23

Combination 4: V12, V21

Combination 5: V12, V22

Combination 6: V12, V23

Combination 7: V13, V21

Combination 8: V13, V22

Combination 9: V13, V23

No combination can ever be repeated.

The number of elements in a valid combination should always be equal to x=2.

Now, the hashmap may also look like this:

Key Value

*** *****

Q1 V11 V12

Q2 V21 V22 V23

Here the value of n is not the same for each collection. This can also be one case.

The elements in the collection may or may not be sorted.

Thanks,

Kunal

M Beck

Ranch Hand

Posts: 323

posted 11 years ago

it wasn't that long ago that a very similar question got asked here... here is the thread.