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()?
Answering
Not answeringthe question, withbuta couple minor points besides about your code: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.