The moose likes Java in General and the fly likes Logical Help needed (All possible combinations) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Logical Help needed (All possible combinations)" Watch "Logical Help needed (All possible combinations)" New topic

Logical Help needed (All possible combinations)

Kunal Sathe
Ranch Hand

Joined: Apr 08, 2005
Posts: 49

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.

Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Your question isn't quiete clear to me.

Qx: Vx1, Vx2, ... Vxn

Can you write down all possible combinations for x=2 and n=3?

Repeating of values is prohibited?
Number of elements in collection is allways n?
Elements are allways sorted ascending in Collection?
Chris Johnston
Ranch Hand

Joined: Dec 13, 2004
Posts: 85
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?
Kunal Sathe
Ranch Hand

Joined: Apr 08, 2005
Posts: 49
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.

M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
it wasn't that long ago that a very similar question got asked here... here is the thread.
I agree. Here's the link:
subject: Logical Help needed (All possible combinations)
It's not a secret anymore!