简体   繁体   English

Java Treemap put方法

[英]Java Treemap put method

I wrote this method. 我写了这个方法。 I don't understand why it is write out this exception: 我不明白为什么写出这个例外:

Exception in thread "main" java.lang.ClassCastException: Kocsma.Sör cannot be cast to java.lang.Comparable 线程“main”中的异常java.lang.ClassCastException:Kocsma.Sör无法强制转换为java.lang.Comparable

Anybody know what is my mistake? 谁知道我的错误是什么?

The compiler reference this line 编译器引用此行

beers.put(beer, dl); beers.put(beer,dl);

This is my code: 这是我的代码:

private Map<Beer, Integer> beers = new TreeMap<Beer, Integer>();


public void Upload(Beer beer, int dl) {
    int d = 0;
    Beer s = null;
    for (Map.Entry<Beer, Integer> item : beers.entrySet()) {
        if (item.getKey().equals(beer)) {
            d = item.getValue();
            s = item.getKey();
        }
    }
    if (s != null) {
        beers.put(s, d + dl);
    }else
    beers.put(beer, dl); // Here is the problem by the Compiler
}

Class Kocsma: 类Kocsma:

public Kocsma() {
    Upload(new Beer("Borsodi sör", 160, 4.6), 1000);
    Upload(new Beer("Pilsner Urquell", 250, 4.4), 800);
    Upload(new Beer("Soproni Ászok", 150, 4.5), 900);
    Upload(new Beer("Dreher Classic", 200, 5.2), 600);
}

Your Beer class needs to implement Comparable<Beer> or you need to provide a Comparator<Beer> to the TreeMap constructor. 您的Beer类需要实现Comparable<Beer>或者您需要为TreeMap构造函数提供Comparator<Beer>

private static class BeerComparator implements Comparator<Beer> {
     @Override
     public int compare(Beer b1, Beer b2) {
         //return a value > 1 if b1 is greater than b2, < 1 if b2 greater than b1,
         //and exactly 0 if the two are equal
     }
}

beers = new TreeMap<Beer, Integer>(new BeerComparator());

TreeMap stores keys by using a binary search tree. TreeMap使用二叉搜索树存储密钥。 This works without effort for some common classes like Integer and String because they are naturally sortable and implement Comparable out of the box. 这对于一些常见的类(如IntegerString不费吹灰之力,因为它们可以自然排序并且可以实现Comparable开箱即用。 However, for your Beer class, you would have to implement it manually. 但是,对于您的Beer类,您必须手动实现它。

If Beer is not a good candidate for comparisons (most things aren't) then consider using HashMap instead, and overriding equals() and hashCode() on Beer (see Effective Java Chapter 3 for a great reference on this). 如果Beer不是比较的好选择(大多数事情不是),那么请考虑使用HashMap ,并覆盖Beer上的equals()hashCode() (请参阅Effective Java Chapter 3以获得对此的一个很好的参考)。

First, Beer must implement the Comparable interface if you want to put it in a TreeMap . 首先,如果要将它放在TreeMapBeer必须实现Comparable接口。 But why do you want a TreeMap ? 但是你为什么想要一个TreeMap You could just use a HashMap . 你可以使用HashMap

Also your code might look much simpler: 您的代码也可能看起来更简单:

private Map<Beer, Integer> beers = new HashMap<Beer, Integer>();

public void Upload(Beer beer, int dl) {
   Integer d  = beers.get(beer);
   if (d != null) {
    beers.put(beer, d + dl);
   }else
    beers.put(beer, dl);
}

In any case you might want to overwrite the equals() (and hashCode() ) method in Beer , so that it equals on its name for example. 在任何情况下,您可能希望覆盖Beerequals() (和hashCode() )方法,以便它例如等于其名称。

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

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