java - Checking the key in a Map -
can suggest me why if condition not working in below code record has key siteid.
while (!pdsxoutrecords.isempty()) { pdsxrecord record = pdsxoutrecords.remove(0); // below if condition not working if(record.getattrs().containskey("siteid")) { system.out.println("testing"); } } and pdsxrecord class this
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; } below thing gets printed using record.getattrs()
{gem.2036=null, gem.2037=null, gem.2038=com.ebay.pdsx.common.pdsxattrvalue@6b306b30, gem.2039=null, gem.10230=null, gem.10117=null, gem.10119=null, gem.10240=null, uid=com.ebay.pdsx.common.pdsxattrvalue@1e501e50, gem.10001=null, gem.10002=com.ebay.pdsx.common.pdsxattrvalue@5d095d09, gem.10003=null, gem.10246=null, gem.10247=null, gem.60001=null, gem.10007=null, gem.10009=null, gem_routing.partnerlastmodifieddate=null, gem.70006=null, cguid=com.ebay.pdsx.common.pdsxattrvalue@1e361e36, gem.10173=null, gem.10097=null, gem.10131=null, gem.10010=null, gem.10132=null, gem.10177=null, gem.10178=null, gem.10179=null, gem.10015=null, timestamp=com.ebay.pdsx.common.pdsxattrvalue@1e571e57, gem.10016=com.ebay.pdsx.common.pdsxattrvalue@645e645e, gem.10018=null, gem.10019=null, gem.2025=null, siteid=com.ebay.pdsx.common.pdsxattrvalue@1e3f1e3f, gem_routing.partner1lastloggedinuserid=null, gem_routing.partner3lastloggedinuserid=null, gem.10181=null, gem.10182=null, gem.10183=null, gem.10185=null, gem.10187=null, gem.10101=null, gem.10189=null, gem.10102=null, gem.10026=null, pguid=com.ebay.pdsx.common.pdsxattrvalue@1e461e46, gem.2032=null, sguid=null, gem.2033=null, gem.2034=null, gem.2035=null} this below pdsxrecord class
public class pdsxrecord { private string m_key; private map<pdsxattrkey, pdsxattrvalue> m_mapattrs; // use other constructor! protected pdsxrecord() { } // constructor public pdsxrecord(string key, map<pdsxattrkey, pdsxattrvalue> mapattrs) { m_key = key; m_mapattrs = mapattrs; } /** * key * * @return */ public string getkey() { return m_key; } /** * attributes map of key=value * * @return */ public map<pdsxattrkey, pdsxattrvalue> getattrs() { return m_mapattrs; } /** * string -- debugging , simple persistence */ public string tostring() { unsynchronizedstringbuffer buf = new unsynchronizedstringbuffer(); buf.append("key=" + getkey() + "\n"); if (getattrs() == null || getattrs().size() == 0) { return buf.tostring(); } (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(); } } updated:- class pdsxattrkey
public class pdsxattrkey { private string m_key; protected pdsxattrkey() { } public pdsxattrkey(string key) { m_key = key; } public string getkey() { return m_key; } /** * override default allow comparing strings */ public boolean equals(object o) { if (o == null) { return false; } if (o instanceof string) { return o.equals(m_key); } if (o instanceof pdsxattrkey) { return m_key.equals(((pdsxattrkey)o).getkey()); } return false; } /** * hash code implementation */ public int hashcode() { return (m_key == null ? 0 : m_key.hashcode()); } public string tostring() { return getkey(); } }
maybe because map consisting of pdsxattrkey key, , checking if there's key string value "siteid".
here's code might useful if not want change map definition map<pdsxattrkey, pdsxattrvalue> map<string, pdsxattrvalue>:
while (!pdsxoutrecords.isempty()) { pdsxrecord record = pdsxoutrecords.remove(0); if(record.getattrs().containskey(new pdsxattrkey("siteid"))) { system.out.println("testing"); } } note assumes can pass string pdsxattrkey constructor, , class can instantiated. oh , of course have equals() , hashcode() in class, pretty check string value of pdsxattrkey. might ask if worth hassle. that's why suggested change map definition use strings keys, of course not sure if viable solution in case.
Comments
Post a Comment