Java 检查地图上的钥匙
有人能告诉我为什么if条件在下面的代码中不起作用,因为记录的key as SiteIdJava 检查地图上的钥匙,java,map,Java,Map,有人能告诉我为什么if条件在下面的代码中不起作用,因为记录的key as SiteId while (!pdsxOutRecords.isEmpty()) { PdsxRecord record = pdsxOutRecords.remove(0); // The below if condition is not working if(record.getAttrs().containsKey("SiteId")) { System.out.println("Testing"); } } P
while (!pdsxOutRecords.isEmpty()) {
PdsxRecord record = pdsxOutRecords.remove(0);
// The below if condition is not working
if(record.getAttrs().containsKey("SiteId")) {
System.out.println("Testing");
}
}
PdsxRecord类是这样的
public class PdsxRecord
{
private String m_key;
private Map<PdsxAttrKey, PdsxAttrValue> m_mapAttrs;
}
// constructor
public PdsxRecord(String key, Map<PdsxAttrKey, PdsxAttrValue> mapAttrs)
{
m_key = key;
m_mapAttrs = mapAttrs;
}
public String getKey()
{
return m_key;
}
public Map<PdsxAttrKey, PdsxAttrValue> getAttrs()
{
return m_mapAttrs;
}
这是下面的PdsxRecord类
public class PdsxRecord
{
private String m_key;
private Map<PdsxAttrKey, PdsxAttrValue> m_mapAttrs;
// use the other constructor!
protected PdsxRecord()
{
}
// constructor
public PdsxRecord(String key, Map<PdsxAttrKey, PdsxAttrValue> mapAttrs)
{
m_key = key;
m_mapAttrs = mapAttrs;
}
/**
* get Key
*
* @return
*/
public String getKey()
{
return m_key;
}
/**
* get attributes as a map of key=value
*
* @return
*/
public Map<PdsxAttrKey, PdsxAttrValue> getAttrs()
{
return m_mapAttrs;
}
/**
* String -- for debugging and simple persistence
*/
public String toString()
{
UnsynchronizedStringBuffer buf = new UnsynchronizedStringBuffer();
buf.append("key=" + getKey() + "\n");
if (getAttrs() == null || getAttrs().size() == 0) {
return buf.toString();
}
for (Map.Entry<PdsxAttrKey, PdsxAttrValue> entry : getAttrs().entrySet()) {
String key = (entry.getKey()==null ? "null" : entry.getKey().getKey());
String value = ((entry.getValue() == null ||
entry.getValue().getValue() == null) ?
"null" : entry.getValue().getValue().toString());
buf.append(" " + key + "=" + value +"\n");
}
return buf.toString();
}
}
可能是因为映射由
PdsxAttrKey
作为键组成,并且您正在检查是否有一个值为“SiteId”的String
键
如果您不想将地图定义从Map
更改为类似Map
,以下代码可能会很有用:
注意,这假设您可以将字符串传递给PdsxAttrKey构造函数,并且可以实例化该类。当然,类中有equals()
和hashcode()
,它们几乎只检查PdsxAttrKey
的字符串值。你可能会问自己,这是否真的值得这么麻烦。这就是为什么我最初建议您更改地图定义,以使用字符串作为键,但我当然不确定这是否是您的情况下可行的解决方案 在record.getAttrs()
中返回:Map
。然后检查是否存在类型为String
、值为“SiteId”的键(类型:PdsxAttrKey
)
您应该检查map是否包含PdsxAttrKey
(在PdsxAttrKey
中实现equals
和hashcode
方法)或是否从PdsxAttrKey
中提取键,并将它们与“SiteId”进行比较
如果选择迭代,请尝试以下操作:
for(PdsxAttrKey key : record.getAttrs().keySet()) {
if("SiteId".equals(key.getYourKeyStringValue())) {
//found
break;
}
}
否则,您应该在PdsxAttrKey
中实现equals
和hashcode
(请记住-两者)并调用contains
:
PdsxAttrKey lookupKey = new PdsxAttrKey("SiteId"); //with consideration of `equals` method
if(record.getAttrs().containsKey(lookupKey)) {
...
}
正如Xeon所说,如果要将对象与字符串值进行比较,则必须重写用作键的类中的
equals
和hascode
方法,在本例中为PdsxAttrKey
。例如:
public class PdsxAttrKey {
public String name;
public PdsxAttrKey(String name) {
this.name = name;
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(Object obj) {
if(obj == null) {
return false;
} else if (obj instanceof PdsxAttrKey) {
return this.name.equals(((PdsxAttrKey)obj).name);
}
return false;
}
}
或者,如果不需要将对象作为键,则可以按如下所示重新定义映射声明,并使用字符串作为键
私有地图m_mapatrs代码>问题是什么?请更清楚地解释,并显示您的期望值和实际得到的值。那么我如何将其作为PdsxAttrKey
进行检查?有什么想法吗?那么我如何才能将其作为PdsxAttrKey
检查呢?有什么想法吗?如果你不需要字符串以外的任何其他功能,那么我说改变你的地图定义。那是一个。否则,不要检查字符串
而是创建一个值为“siteId”的PdsxAttrKey
,并确保你的PdsxAttrKey
类有一个equals()
方法,如果不知道PdsxAttrKey
类,我们无法判断。如果幸运的话,它可以由一个字符串
构造,并适当地实现equals()
和hashcode()
。我通过发布完整的PdsxRecord类编辑了我的问题。让我知道这是否对您有帮助,以及我是否可以通过查看该类来使用您发布的代码?如果您想通过字符串
进行查找,那么它应该是映射
。
for(PdsxAttrKey key : record.getAttrs().keySet()) {
if("SiteId".equals(key.getYourKeyStringValue())) {
//found
break;
}
}
PdsxAttrKey lookupKey = new PdsxAttrKey("SiteId"); //with consideration of `equals` method
if(record.getAttrs().containsKey(lookupKey)) {
...
}
public class PdsxAttrKey {
public String name;
public PdsxAttrKey(String name) {
this.name = name;
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(Object obj) {
if(obj == null) {
return false;
} else if (obj instanceof PdsxAttrKey) {
return this.name.equals(((PdsxAttrKey)obj).name);
}
return false;
}
}