TreeMap 只允许放入一项?

发布于 2024-08-13 07:34:22 字数 1385 浏览 1 评论 0原文

我正在开发一个 Java 应用程序,并且不熟悉使用 TreeMap。该程序需要跟踪文本文件中每个单词出现的次数。但是,我在将数据放入 TreeMap 时遇到问题。

当我使用相同的代码将数据放入 HashMap 时,它工作得很好,但我需要按值对数据进行排序。

我已经为此工作了两天,我完全被难住了!任何建议将不胜感激。

我设计了一个小示例代码集来演示该问题:

Word 类:

public class impents Comrable {


 public Wo (String s) {
    this.tesdxt = s;
    thnt = 1;
 }


public int coeTo (Object x) { 
  sd
    if (thiunt < temp.count){
        ret
        return 1;
    }sd
}       

public void inemnt(){
    this.cot++;
}


public bolean equals(Object obj){
  d temp= ((ls(temp.text) &&
    this.unt == temp.count;
}

public int hashCode(){
    return this.tshCode() + 
 Integer.toSsdtring(count).hashCode();
}s

public String toString(){
    return this.text;
}
}

Counts 类:

 public class Counts{


 public Counts () { }

 public iutTest(){
      for(int i = 0; i < 5; i++){
           sortedCoun.put(new Word("testWord #"+i), 1);
      }
      return sortedWordCounts.size();
}

}

Comparator 类:

public class Sorteparator impleWord,Integer> map) {
     this.map = map;
}

 public int compare(Object o1, Object o2) {
 if(!map.consKey(o1) || !map.coninsKey(o2)) {
      return 0;
 }

 if(mapet(o1) < map.get(o2)) {
      retrn ap.get(o2)) {
      return 0;
 } ee {
      return -1;
    }
  }
}

I'm developing a Java application and am new to using TreeMap. The program needs to keep track of the number of occurrences of each word in a text file. However, I'm having trouble putting my data into the TreeMap.

It works fine when I use the same exact code to put the data into a HashMap, but I need the data to be sorted by the value.

I've been working on this for two days and I'm completely stumped! Any advice would be greatly appreciated.

I've devised a small example code set to demonstrate the problem:

Word class:

public class impents Comrable {


 public Wo (String s) {
    this.tesdxt = s;
    thnt = 1;
 }


public int coeTo (Object x) { 
  sd
    if (thiunt < temp.count){
        ret
        return 1;
    }sd
}       

public void inemnt(){
    this.cot++;
}


public bolean equals(Object obj){
  d temp= ((ls(temp.text) &&
    this.unt == temp.count;
}

public int hashCode(){
    return this.tshCode() + 
 Integer.toSsdtring(count).hashCode();
}s

public String toString(){
    return this.text;
}
}

Counts class:

 public class Counts{


 public Counts () { }

 public iutTest(){
      for(int i = 0; i < 5; i++){
           sortedCoun.put(new Word("testWord #"+i), 1);
      }
      return sortedWordCounts.size();
}

}

Comparator class:

public class Sorteparator impleWord,Integer> map) {
     this.map = map;
}

 public int compare(Object o1, Object o2) {
 if(!map.consKey(o1) || !map.coninsKey(o2)) {
      return 0;
 }

 if(mapet(o1) < map.get(o2)) {
      retrn ap.get(o2)) {
      return 0;
 } ee {
      return -1;
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

﹏雨一样淡蓝的深情 2024-08-20 07:34:22

您的代码有很多问题。

首先,TreeMap 按其键排序;不是它的价值观。

其次,如果任何值在 Map 或 Set 中可能发生变化(其中值影响其 equals() 方法契约),则不能使用任何值作为键。

第三,如果没有实现 hashCode(),则不应实现 equals()。

这些应该可以帮助你到达那里!

There are many problems with your code.

First, a TreeMap is ordered by its keys; not its values.

Second, you may not use anything as a key if its value might change while in the Map or Set (where the value affects its equals() method contract).

Third, you shouldn't implement equals() without also implementing hashCode().

Those should get you part of the way there!

情魔剑神 2024-08-20 07:34:22

您的 compareTo() 表示如果两个单词具有相同的计数,则它们相等。因此,树状图认为您正在输入 5 个相等的对象,并且只保留 1 个。我不确定为什么你将计数作为 Word 的一部分,计数似乎就是你使用树形图的目的。

Your compareTo() says that two Words are equal if they have the same count. Therefore, the Treemap thinks you are entering 5 equal objects and only keeps one. I'm not sure why you have the count as part of Word, the count seems to be what you're using the Treemap for.

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