When I added my datas on the treeSet I lost almost all my datas. It seems that I have only the first element of my set.
I read this question Hashset vs Treeset for an code optimization and I tried to do something like them. But I didn't really succeed.
Input :
int iValue = 0;
HashSet<TagResult> results = new HashSet<TagResult>();
for(Document doc : docs) {
NodeList nList = doc.getElementsByTagName("TEXT");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
iValue = searchWords.searchOnTag(eElement, szSearch);
if(iValue > 0) {
results.add(new TagResult(eElement, iValue));
}
}
}
}
System.out.println("Set :\n-------------------------");
for(TagResult result : results) {
System.out.println(getTagValue("Tag",result.getElement()));
}
Set<TagResult> sortedResult = new TreeSet<TagResult>(new Comparator<TagResult>() {
public int compare(TagResult e1, TagResult e2) {
return e2.getValue() - e1.getValue();
}
});
sortedResult.addAll(results);
System.out.println("Sorted Result :\n-------------------------");
for(TagResult result : sortedResult) {
System.out.println(getTagValue("Tag",result.getElement()));
}
The execution result give me that :
Output
Set :
TXT_KEY_RELIGION_POSITIF_MEDIAN_05
TXT_KEY_RELIGION_POSITIF_MEDIAN_02
TXT_KEY_RELIGION_POSITIF_04
TXT_KEY_RELIGION_POSITIF_06
TXT_KEY_RELIGION_POSITIF_05
TXT_KEY_RELIGION_POSITIF_03
TXT_KEY_RELIGION_POSITIF_MEDIAN_06
TXT_KEY_RELIGION_POSITIF_MEDIAN_01
TXT_KEY_RELIGION_POSITIF_MEDIAN_04
TXT_KEY_RELIGION_POSITIF_MEDIAN_08
TXT_KEY_RELIGION_POSITIF_MEDIAN_03
TXT_KEY_RELIGION_POSITIF_02
TXT_KEY_RELIGION_POSITIF_07
TXT_KEY_RELIGION_POSITIF_01
TXT_KEY_RELIGION_POSITIF_MEDIAN_09
TXT_KEY_RELIGION_POSITIF_MEDIAN_07
Sorted Result :
TXT_KEY_RELIGION_POSITIF_MEDIAN_05
I don't understand why I have this problem.
I would imagine that
e2.getValue() - e1.getValue();
returns 0 for all the items in your set which are then considered equal. Try printing tag.getValue()
for your tags to check if it is the case.
Extract from TreeSet
javadoc (emphasis mine):
Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With