I have a very strange problem that has some limitations that make it difficult to solve. I have a list of lists, and I want to make combinations of all the elements in these lists. Each element has a name and a value. Here is an example:
Main list:
- List 01:
- Item 01: name: name01, value: value01
- Item 02: name: name02, value: value02
- List 02:
- Item 01: name: name03, value: value03
- List 03:
- Item 01: name: name04, value: value04
- Item 02: name: name05, value: value05
The end result should look like this:
Some listings:
- Item 01: name01: value01, name03: value03, name04: value04
- Item 02: name02: value02, name03: value03, name04: value04
- Item 03: name03: value03, name03: value03, name04: value04
- Item 04: name01: value01, name03: value03, name04: value05
- Item 05: name02: value02, name03: value03, name04: value05
- Item 06: name03: value03, name03: value03, name04: value05
The new list contains items that act as a hash map.
The limitations are as follows:
- I cannot compile new lists and mix them, because these lists can grow quite large quite quickly.
- I use some kind of observer-like api, so I need to inform the observer of the result as soon as possible so that I do not use a lot of memory.
In other words, the combination generator can be equipped with X number of lists, each of which can contain N number of elements, and I have to generate their combinations without using too much memory.
I do not expect to work with more than 5 lists at a time, but I would like to make the algorithm as stable as possible for changing the code.
I am solving a problem in java, but the algorithm should work the same in other languages, because it is likely to be translated.
Do you have any ideas, suggestions?
Thanks in advance.
PS I do not think recursion will work well. I play with the idea of ββusing a while loop and some nested loops, but it becomes very difficult to imagine how this should work.
source share