Java TreeMap put() 方法的奇怪行为
我有以下代码,它将 Vector 分成字符串向量(用作键)和末尾的整数(用作值)。
payoffs.put(new Vector<String>(keyAndOutput.subList(0, keyAndOutput.size() - 1)), Integer.parseInt(keyAndOutput.lastElement()));
所讨论的 TreeMap 是使用具有以下方法的 Comparator 构建的,该方法强制执行字典顺序、与大小写无关的排序,并且还考虑了长度(较长的向量总是比较短的向量“更大”)。
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;
}
现在,对于问题...尽管正在读取的文本文件中有 8 个条目,但地图最多只能获取 2 个条目。 一旦输入一个条目(键),它就会保留,但值会随着扫描过程的每次迭代而变化(每次从文件中的一行读取新向量时)。 它会丢弃除两个之外的所有其他键。
这是我的比较器的问题吗? 或者 TreeMap 是否用 put() 做了一些我不理解的事情?
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()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答
不回答问题,除了关于您的代码之外,还使用但是几个小要点:编辑: 噢! 当然,String.compareToIgnoreCase() 的返回是这 (3) 个比较器之一。 正如我指出的同时发布的另一个答案,这可能是导致您错误的原因。
编辑2:更正了开头陈述以反映问题的实际答案。
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.
我不知道这是否是问题的原因,但 Java 中的比较函数通常返回负数或正数或 0,不一定是 1 或 -1。
我愿意打赌,您会以某种方式从compareToIgnoreCase获得一个非零值,但因为它不是1或-1,所以您会失败,最终返回0,即使数组的长度和内容相同。 尝试检查 >0 和 <0
另外,您可以更好地组织此代码。 例如,进行一项比较,保存结果,然后打开结果。 这样你可能会徒劳地进行两次昂贵的比较。
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.
实际上,诀窍可能确实是我误读了compareTo()的文档实际上所说的...将在测试后报告。
啊,就是这样。 谢谢人们。
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.