【发布时间】:2021-02-05 23:28:31
【问题描述】:
您好,我有 3 个属性(字符串)用于识别某个对象。
什么是更好的解决方案:
使用密钥对象作为Pierre 和Joachim Sauer 的帖子,如下所示:
public class MyKey{
private String one;
private String two;
private String three;
public MyKey(Sting one,String two, String three){
this.one=one;
this.two=two;
this.three=three;
}
//getter only
@Override
public int hashcode(){
// creating Hashcode
}
@Override
public boolean equals(Object o){
// comparing
}
}
=> Map<MyKey, Object>
或在地图中使用地图中的地图:
=> Map<String, Map<String, Map<String, Object>>>
【问题讨论】:
-
你都试过了吗?哪一个感觉更舒服?
-
当然第一种方式更好。当涉及到泛型参数类型的嵌套时,您永远不应该超过两个层次。
-
@Rohit - 从来没有?曾经吗?
标签: java