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

发布于 2024-07-11 00:56:27 字数 1187 浏览 7 评论 0原文

我有以下代码,它将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不可一世的女人 2024-07-18 00:56:28

回答不回答问题,除了关于您的代码之外,还使用但是几个小要点:

  1. 您不应该执行compareTo两次; 比较一次,将结果赋给sgn; then break if !=0
  2. 你的 else continue 是多余的。
  3. 您不应比较 -1 或 1,而应比较 <0 或 >0; 许多compareTo方法基于(x1-x2)返回,它可以给出任何负数或正数。

编辑: 噢! 当然,String.compareToIgnoreCase() 的返回是这 (3) 个比较器之一。 正如我指出的同时发布的另一个答案,这可能是导致您错误的原因。

编辑2:更正了开头陈述以反映问题的实际答案。

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 is 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 negative or positive 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.

若能看破又如何 2024-07-18 00:56:28

我不知道这是否是问题的原因,但 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.

假装不在乎 2024-07-18 00:56:28

实际上,诀窍可能确实是我误读了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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文