寻找“连锁地图” Java 中的实现
我需要从键列表到值的映射。我知道我可以像这样编写自己的代码:
Map<Person, Map<Daytime, Map<Food, Integer>>> eaten = ...;
现在我想要一些 get
和 put
方法,如下所示:
Integer numberOfEggsIAteInTheMorning = eaten.get(me, morning, scrambledEggs);
eaten.put(me, evening, scrambledEggs, 1);
你知道有这样的现有类吗API?我自己也懒得写了。 ;)
I need a mapping from a list of keys to a value. I know I could write my own code like this:
Map<Person, Map<Daytime, Map<Food, Integer>>> eaten = ...;
Now I want to have some get
and put
methods like these:
Integer numberOfEggsIAteInTheMorning = eaten.get(me, morning, scrambledEggs);
eaten.put(me, evening, scrambledEggs, 1);
Do you know of an existing class that has this kind of API? I'm too lazy of writing it myself. ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您寻找更通用的方法,并且可能有超过 2 或 3 个“链步骤”,我建议应用一些不同的结构方法,而不是坚持仅使用基本集合类。我觉得如果正确应用,复合模式可能是正确的选择。
编辑:由于请求示例
完整的示例会有点耗时,所以让我用肮脏的Java/伪代码混合来解释我的想法(我什至不确定我是否错过了一些东西! !)。假设我们有 BaseMap 类:
那么我们可以有 ObjectMap,它将成为复合结构的“叶子”:
实际的复合结构将如下所示:
If you look for a more generic approach, and you might have more than 2 or 3 'chain steps', I would suggest in applying some different structural approach, rather than sticking to using only basic collection classes. I have feeling that Composite Pattern could be the right choice if it's correctly applied.
EDIT: due to example requested
The full example would be somewhat time consuming, so let me just explain my idea with dirty Java/pseudocode mix (I'm not even sure if I've missed something!!!). Let's consider we have class BaseMap:
Then we could have ObjectMap that would be the 'leaf' of our composite structure:
And the actual composite would be as such:
您可以使用 org.apache.commons.collections.keyvalue.MultiKey 来实现:Map
You can use
org.apache.commons.collections.keyvalue.MultiKey
for that:Map<Multikey, Object>
实现通用的链式地图是很困难的。
类的声明会是什么样子? (您不能拥有可变数量的类型参数。
另一种选择是使用一个
ChainedMapUtil
类来递归执行 put / get。这是一个递归 get 的示例。(尽管解决方案相当丑陋)我必须说。)
It would be hard to implement a general chained map.
How would the declaration of the class look like? (You can't have a variable number of type parameters.
Another option would be to have a
ChainedMapUtil
class that performs put / get recursively.Here is an example of a recursive get. (Quite ugly solution though I must say.)
如果您要编写自己的键,我建议
您可以使用复合键
(TObjectIntHashMap 支持增量和调整)
您甚至可能不需要自定义键。
使用 split() 分解密钥相当容易
If you are going to write your own, I would suggest
You could use a composite key
(TObjectIntHashMap supports increment and adjust)
You may not even need a custom key.
It is fairly easy to decompose the key with split()
我曾经使用 3 个键制作了一个地图只是为了好玩。也许你可以使用它而不是使用链式地图:
更新:
正如 @Arturs Licis 所建议的。我在网上查找了复合模式,然后写了使用它的示例。我猜这是复合的。如果不是这样,请发表评论。
人物类别:
食物类别:
时间类别:
I once made a map using 3 keys just for fun.May be you can use it instead of using chained maps:
UPDATE:
As @Arturs Licis suggested.I looked up in net for composite pattern and I wrote a sample using it.I guess this is composite..Please comment if it is not so.
Person class:
Food class:
Time class: