views:

722

answers:

3

I have the following code, which splits up a Vector into a string vector (to use as a key) and an integer at the end (to use as value).

payoffs.put(new Vector<String>(keyAndOutput.subList(0, keyAndOutput.size() - 1)), Integer.parseInt(keyAndOutput.lastElement()));

The TreeMap in question is constructed using a Comparator with the following method, which imposes a lexicographic, case independent ordering that also takes length into account (longer vectors are always "greater" than shorter ones).

public int compare(Vector<String> arg0, Vector<String> arg1) {
     int sgn = 0;
     if (arg0.size() > arg1.size()) {
      return 1;
     } else if (arg0.size() < arg1.size()) {
      return -1;
     }
     for (int i = 0; i < arg0.size(); i++) {
      if (arg0.elementAt(i).compareToIgnoreCase(arg1.elementAt(i)) == 1) {
       sgn = 1;
       break;
      } else if (arg0.elementAt(i).compareToIgnoreCase(arg1.elementAt(i)) == -1) {
       sgn = -1;
       break;
      } else {
       continue;
      }
     }
     return sgn;
    }

Now, for the problem...despite having 8 entries in the text file this is being read from, the map only ever gets up to 2 entries. Once one entry (key) is entered, it STAYS, but the VALUE changes with every iteration of the scanning process (every time it reads in a new vector from a line in the file). It throws out all the other keys except for the two.

Is this a problem with my comparator? Or is the TreeMap doing something I don't understand with put()?

+2  A: 

I don't know if this is the cause of your problem, but compare functions in Java usually return negative or positive or 0, not necessarily 1 or -1.

I am willing to bet that you are somehow getting a nonzero value from compareToIgnoreCase, but because it's not 1 or -1 you fall through, and end up returning 0 even though the arrays are identical in length and not content. Try checking against >0 and <0

Also, you can organize this code better. For example, do one comparison, save the results, then switch on the results. This way you may be doing two expensive compares for nothing.

Uri
+2  A: 

Answering Not answering the question, with but a couple minor points besides about your code:

  1. You should not do the compareTo twice; compare once and assign the result to sgn; then break if !=0
  2. Your else continue it redundant.
  3. You should not compare for -1 or 1, but <0 or >0; many compareTo methods return based on (x1-x2), which can give any neg or pos number.

EDIT: Doh! And, of course, the return for String.compareToIgnoreCase() is one of those (3) comparators. As the other answer posted at the same time as mine pointed out, that will likely be the cause of your error.

EDIT2: Corrected opening statement to reflect question was actually answered.

Software Monkey
A: 

Actually, the trick may indeed be that I misread what documentation for compareTo() actually said...will report once tested.

Aaand, that was it. Thanks people.

Alexander