Map< K1,K2,V>的任何实现,即两个键? [英] Any implementation of Map<K1, K2, V>, i.e. two keys?

查看:250
本文介绍了Map< K1,K2,V>的任何实现,即两个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个包含两个键的映射,例如

  Map2< String / * ssn * /,String / * empId * /,员工> _雇员; 

让我可以

  _employees.put(e.ssn(),e.​​empId(),e)

然后

  _employees.get1(someSsn); 
_employees.get2(someImpId);

甚至

  _employees.remove1(someImpId); 

我不确定为什么我想停在两点,为什么不多点,可能是因为情况如此我是现在需要的:-)但类型需要处理固定数量的键是类型安全的 - 类型参数不能是可变参数: - )

Appreciate任何指针或建议为什么这是一个坏主意。

解决方案

我的第一个想法是:最简单的方法来做到这一点,我想,这将是两张地图。

 地图<字符串,地图<串,雇员> > _雇员; 

但是从它看起来,您只希望能够通过SSN或ID。那么,制作两张地图,或者最糟糕的是一个包含两张地图的课程会阻止你?

作为澄清,您是否在寻找一个复合键:员工是通过其SSN和ID的组合唯一标识的,但不是一个人自己,还是你在寻找两种不同的方式来引用员工?


I need a map that has two keys, e.g.

Map2<String /*ssn*/, String /*empId*/, Employee> _employees;

So that I can

_employees.put(e.ssn(), e.empId(), e)

And later

_employees.get1(someSsn);
_employees.get2(someImpId);

Or even

_employees.remove1(someImpId);

I am not sure why I want to stop at two, why not more, probably because that's the case I am I need right now :-) But the type needs to handle fixed number of keys to be type-safe -- type parameters cannot be vararg :-)

Appreciate any pointers, or advice on why this is a bad idea.

解决方案

My first thought was: the easiest way to do this, I think, would be two maps.

Map< String, Map< String,Employee> > _employees;

But from what it looks like, you just want to be able to look up an employee by either SSN or ID. What's to stop you then from making two maps, or at worst a class that contains two maps?

As a clarification, are you looking for a compound key being employees are uniquely identified by the combination of their SSN and ID, but not either one by itself, or are you looking for two different ways of referencing an employee?

这篇关于Map&lt; K1,K2,V&gt;的任何实现,即两个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆