Hi César
The default implementations of nodes in dom4j uses identity based hashCode()
and equals() methods. This is primarily for performance so that using Nodes
as keys in, say, a HashMap would be fast and efficient using identity based
lookup.
To implement value based equality would require calls to hashCode() or
equals() of a document to traverse the entire document structure evaluating
the hashCode() / equals() on each Element, Attribute and Text node etc.
If you want to do value based comparisons, there's a comparator called
NodeComparator which can be used if ever you wanted some value based lookup,
such as storing a set of unique attributes objects compared to via value
rather than identity.
http://dom4j.org/apidocs/org/dom4j/util/NodeComparator.html
So you could build a value based Set of nodes using the following code:-
Set set = new TreeSet( new NodeComparator() );
Then to find all the unique values of a certain Attribute you could do
set.addAll( document.selectNodes( "//@foo" ) );
System.out.println( "Number of distinct values of @foo are: " +
set.size() );
James
----- Original Message -----
From: "César Álvarez Núñez" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, July 06, 2001 1:57 PM
Subject: [dom4j-dev] Why not are implemented the hashCode & equals methods?
Working with Attributes and list of attributes, I've realized that
AbstractNode class do not implement the hashCode or equals methods.
Why? They are necessary in order to work with collections and be able to
compare objects.
For example if you create two identical objects and compare them with
equals, the result is false.
org.dom4j.tree.DefaultAttribute obj1 = new
org.dom4j.tree.DefaultAttribute("name","Peter");
org.dom4j.tree.DefaultAttribute obj2 = new
org.dom4j.tree.DefaultAttribute("name","Peter");
boolean equal = obj1.equals(obj2);
// It returns 'false', when it should return 'true'.
Regards,
César.
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
_______________________________________________
dom4j-dev mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dom4j-dev