How to create combinations of values in Java?

Viewed 2595

I have the following map: Map<Integer,String[]> map = new HashMap<Integer,String[]>();

The keys are integers and the values are arrays (could also be replaced by lists).

Now, I would like to get all possible combinations of the values among the keys. For example, let's say the map contains the following entries:

key 1: "test1", "stackoverflow"
key 2: "test2", "wow"
key 3: "new"

The combinations consists of

("test1","test2","new")
("test1","wow","new")
("stackoverflow", "test2", "new")
("stackoverflow", "wow", "new")

For this I imagine a method boolean hasNext() which returns true if there is a next pair and a second method which just returns the next set of values (if any).

How can this be done? The map could also be replaced by an other data structure.

2 Answers
Related