简体   繁体   English

Java的TreeMap put()方法的奇怪行为

[英]Strange behavior of Java's TreeMap put() method

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). 我有以下代码,它将Vector分成一个字符串向量(用作键)和一个末尾的整数(用作值)。

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). 所讨论的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;
    }

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. 现在,对于问题...尽管在文本文件中有8个条目正在被读取,但是地图最多只能获得2个条目。 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). 输入一个条目(键)后,它将保持不变,但是VALUE在扫描过程的每次迭代中都会更改(每次它从文件中的一行中读取新矢量时)。 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()? 还是TreeMap用put()做我不了解的事情?

Answering Not answering the question, with but a couple minor points besides about your code: 回答 不回答 这个问题,有 除了你的代码中的几个小点:

  1. You should not do the compareTo twice; 您不应两次执行compareTo; compare once and assign the result to sgn; 比较一次并将结果分配给sgn; then break if !=0 如果!= 0则中断
  2. Your else continue is redundant. 您的其他继续是多余的。
  3. You should not compare for -1 or 1, but <0 or >0; 您不应将-1或1进行比较,而应将<0或> 0进行比较; many compareTo methods return based on (x1-x2), which can give any negative or positive number. 许多compareTo方法基于(x1-x2)返回,可以给出任何负数或正数。

EDIT: Doh! 编辑:Doh! And, of course, the return for String.compareToIgnoreCase() is one of those (3) comparators. 而且,当然,String.compareToIgnoreCase()的返回值是这三个比较器之一。 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. EDIT2:更正了开篇陈述以反映问题,实际上已得到答复。

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. 我不知道这是否是造成您问题的原因,但是Java中的比较函数通常返回负数或正数或0,不一定返回1或-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. 我愿意打赌,您以某种方式从compareToIgnoreCase中获取了一个非零值,但是由于它不是1或-1,因此您会失败,即使数组长度相同且内容不相等,最终也会返回0。 Try checking against >0 and <0 尝试检查> 0和<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. 这样,您可能会做两个昂贵的比较。

Actually, the trick may indeed be that I misread what documentation for compareTo() actually said...will report once tested. 实际上,技巧可能确实是我误读了compareTo()的实际文档说的……一旦测试将报告。

Aaand, that was it. 阿兰,就是这样。 Thanks people. 谢谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM