我有一个twoKeys类,该类具有equal()和hashcode()以及2个数据包装器。 现在,在服务类中,如何调用具有1个值的2个键。第一个将被硬编码,另一个将被迭代。我需要什么代码?
data1Wrapper wrapper = new data1Wrapper(data1);
data2Wrapper wrapper2 = new data2Wrapper(data2);
Map<String, List<data1>> maps1 = wrapper.getData();
Map<twoKeys, List<data2>> maps2 = wrapper2.getData();
for (Map.Entry<String, List<data1>> entry1 : maps1.entrySet()) {
String key = entry1.getKey();
List<data2> values1 = entry1.getValue();
// I have a problem below this line about calling the value with 2 keys
List<data2> values2 = maps2.get(key);
compareData(values1, values2);
}
第一个键为“ APPROVED”或“ DENIED”,第二个键为唯一ID。
twoKeys
private String status;
private String uid;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getUid() {
return uid;
}
public void setUid(String part) {
this.uid = uid;
}
@Override
public String toString() {
return "twoKeys{" + "status=" + status + ", uid=" + uid + '}';
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
twoKeys key = (twoKeys) obj;
return status.equals(key.status)
&& uid.equals(key.uid);
}
@Override
public int hashCode() {
int hash = 1;
hash = hash * 23 + (status == null ? 0 : status.hashCode());
hash = hash * 23 + (part == null ? 0 : uid.hashCode());
return hash;
}
答案 0 :(得分:0)
当预期的键类型为twoKeys类时,从map2检索值时不能传递String对象。
您将必须使用构造函数来创建twoKeys对象,
twoKeys twoKeysObj = new twoKeys("somestatus", "someuid");
然后您就可以调用这样的方法
List<data2> values2 = maps2.get(twoKeysObj );
代替这个
List<data2> values2 = maps2.get(key);
希望这会有所帮助。
Edit1:
您需要在twoKeys类中创建此构造函数。
public twoKeys(String status, String uid) {
this.status = status;
this.uid = uid;
}
或者如果您不想创建此构造函数,则可以执行此操作,
twoKeys twoKeysObj = new twoKeys();
twoKeysObj.setStatus("somestatus");
twoKeysObj.setUid("someuid");