不明白为什么一个文档的两个org.w3c.dom.NodeList不相等,而它们的哈希码也不相等。尽管一个NodeList中的每个Node等于另一个NodeList中的该Node? p>
// Parse xml file to Document
File fXmlFile = new File("src/test/resources/sample.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
// Get cais Nodes
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList caisNodes = (NodeList) xpath.evaluate("//c[@n='CAIS']/s", doc, XPathConstants.NODESET);
NodeList caisNodes2 = (NodeList) xpath.evaluate("//c[@n='CAIS']/s", doc, XPathConstants.NODESET);
System.out.println(caisNodes.hashCode() == caisNodes2.hashCode()); //false
System.out.println(caisNodes.equals(caisNodes2)); //false
for (int i = 0; i < caisNodes.getLength(); i++) {
System.out.println(caisNodes.item(i).equals(caisNodes2.item(i))); // all true
System.out.println(caisNodes.item(i).hashCode()==caisNodes2.item(i).hashCode()); // all true
}
答案 0 :(得分:0)
当您尝试比较两个对象时,您正在比较的是引用而不是值。两者都存在于内存中的不同位置并具有不同的地址。因此,当您对它们应用相等运算符时,它将返回false。 I-E 0xA332CD == 0xB2254F //错误