Saturday 15 March 2014

Java TreeMap containsKey invariably returning true? -


I am writing a Java program which is using the TreeMap interface, and I have some problem in it . This fact is also returning when I give something like something that I know there is nothing in the treemark.

What could be the reason?

Thank you very much earlier.

-

Edit: I am writing a program that counts the occurrence of words in a text file. Words are parsed and each is a new example of a class. In these classes, equal and hashcode methods are overridden because words are considered equal, even if they are different objects.

The field "text" is a string that contains the word text.

  public boolean par (object obj) {word temp = ((word) obj); Return this return.text.equals (temp.text); } Public Action Hashod () {return.text.hashCode (); } To compare Public Int (object x) {Word temp = ((Word) X); If (this.text.compareTo (temp.text)) & lt; 0) {return -1; } And if (this. Text.equals (temp.text)) {return 0; } And {return1; }}  

My guess is that you are using a key type in which false Implementation of equals (and possibly hashcode ), or that comparator does not correspond to equal

if you We can prepare a small but complete program that demonstrates the problem, so we can confirm it.


No comments:

Post a Comment