我想知道解决以下问题的最佳方法是什么:
我现在有一个 Map>
。假设它是一张餐食地图,其中包含您可以在该餐食中提供的一系列饮料。例如
早餐-橙汁
早餐 - 咖啡
午餐 - 橙汁
午餐 - 苏打水
午餐 - 啤酒
晚餐 - 苏打水
晚餐 - 啤酒
晚餐 - 酒
所以我的收藏实际上是Map>
我需要做的是创建一个List
餐食中各种饮料的组合。在本例中我将有 18 种组合。例如
[
[
早餐->橙汁,
午餐->橙汁,
晚餐->苏打
]
[
早餐->橙汁,
午餐->橙汁,
晚餐->啤酒
]
[
早餐->橙汁,
午餐->橙汁,
晚餐->葡萄酒
]
[
早餐->橙汁,
午餐->苏打,
晚餐->苏打,
]
ETC...
]
我有兴趣看看其他人将如何创建最终的系列。
另外,我使用的是java,所以漂亮的功能性东西是禁止的。
谢谢
编辑
膳食类型是动态的。也就是说,可以从列表中删除晚餐或添加早午餐。
I'm wondering what the best way of solving the following problem is:
I have at the moment a Map<String, Collection<String>>
. Let's say its a map of meals with a collection of beverages you could serve at that meal. E.g.
Breakfast - Orange Juice
Breakfast - Coffee
Lunch - Orange Juice
Lunch - Soda
Lunch - Beer
Dinner - Soda
Dinner - Beer
Dinner - Wine
So my collection really is Map<Meal, Collection<Beverage>>
What I need to do is create a List<Map<String,String>>
of all the various combinations of beverages over the meals. I will have in this case 18 combinations. E.g.
[
[
Breakfast -> Orange Juice,
Lunch -> Orange Juice,
Dinner -> Soda
]
[
Breakfast -> Orange Juice,
Lunch -> Orange Juice,
Dinner -> Beer
]
[
Breakfast -> Orange Juice,
Lunch -> Orange Juice,
Dinner -> Wine
]
[
Breakfast -> Orange Juice,
Lunch -> Soda,
Dinner -> Soda,
]
etc...
]
I'm interested in seeing how others would go about creating the final collection.
Also, I'm using java, so nifty functional stuff is off limits.
Thanks
EDIT
Meal types are dynamic. That is, dinner could be removed from the list or brunch added.
发布评论
评论(2)
如果我没猜错的话,这里没有魔法,只需在嵌套循环中迭代 3 个集合即可。
我确信现在您可以弄清楚如何使用
Map
和输出List
来实现这一点。 :-)编辑:对于更改的需求,一种方法是递归:
未经测试,但您明白了。
If I got it right, there's no magic here, just iterate over 3 collections in a nested loop.
I'm sure now you can figure out how to do it with your
Map
and outputList
. :-)EDIT: For the changed requirements, one way to do it is recursion:
Not tested, but you get ths idea.
使用一系列柜台,柜台数量与餐点数量相同。循环增加第一个饮料,直到达到第一顿饭的最后一杯饮料,然后增加下一个饮料(滴下)。当最后一顿饭的最后一杯饮料喝完时,你就完成了。
我将把代码留给你。
Use an array of counters with as many counters as there are meals. In a loop increase the first one until it reaches the last beverage for the first meal and then increase the next on (trickling down). When the last beverage of the last meal is reached you are done.
I'll leave the code for that up to you.