java:TreeSet 集合和可比较接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13438191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:43:05  来源:igfitidea点击:

java : TreeSet Collection and Comparable interface

javacollections

提问by mcacorner

I have following code : I am trying to insert Item object in TreeSet and I am not getting desire output.

我有以下代码: 我试图在 TreeSet 中插入 Item 对象,但没有得到想要的输出。

public class Main
{
    public static void main(String a[])
    {
        Item i1=new Item(1,"aa");
        Item i2=new Item(5,"bb");
        Item i3=new Item(10,"dd");
        Item i4=new Item(41,"xx");
        Item i5=new Item(3,"x5");
        TreeSet t=new TreeSet();    
        t.add(i1);
        t.add(i2);
        t.add(i3);
        t.add(i4);
        t.add(i5);
        System.out.println(t);      
    }
}
class Item implements Comparable<Item>
{
    String  nm;
    int price;
    public Item(int n,String nm)
    {
        this.nm=nm;
        price=n;
    }
    public int compareTo(Item i1)
    {
        if(price==i1.price)
            return 0;
        else if(price>=i1.price)
            return 1;
        else
            return 0;
    }
    public String  toString()
    {
        return "\nPrice "+price+" Name : "+nm;
    }    
}

Output :

输出 :

[ Price 1 Name : aa,
Price 5 Name : bb,
Price 10 Name : dd,
Price 41 Name : xx ]

[价格1名称:aa,
价格5名称:bb,
价格10名称:dd,
价格41名称:xx]

Item i5=new Item(3,"x5");is not Inserted why?
Why I can do to insert in TreeSet.

Item i5=new Item(3,"x5");没有插入为什么?
为什么我可以在 TreeSet 中插入。

回答by Andrew Logvinov

You haven't implemented compareTo()correctly. Here's an extract from javadoc:

你没有compareTo()正确实施。这是javadoc的摘录:

Compares this object with the specified object for order. Returns a negative integer,
zero, or a positive integer as this object is less than, equal to, or greater than
the specified object.

Your implementation doesn't return -1in case price of current object is less than price of object you compare with.

-1如果当前对象的价格低于您比较的对象的价格,则您的实现不会返回。

回答by Muel

In your compareTomethod you should have else return -1;!

在你的compareTo方法中你应该有else return -1;

回答by Mordechai

In compareToreplace:

compareTo替换:

else
        return 0;

with:

和:

else
        return -1;

回答by onon15

A class implementing Comparablemust comply with the contract: if a.compareTo(b) < 0then b.compareTo(a) > 0. Yours does not comply.

一个类实现Comparable必须遵守契约: if a.compareTo(b) < 0then b.compareTo(a) > 0。你的不符合。

回答by Rais Alam

Main problem is in your compareTo method. You have implemented a wrong logic. I have modified your class and tested at my eclipse it giving desired output. look at below code.

主要问题出在您的 compareTo 方法中。你实现了错误的逻辑。我已经修改了您的课程并在我的日食中进行了测试,它提供了所需的输出。看看下面的代码。

import java.util.TreeSet;
public class Main
{
    public static void main(String a[])
    {
        Item i1=new Item(1,"aa");
        Item i2=new Item(5,"bb");
        Item i3=new Item(10,"dd");
        Item i4=new Item(41,"xx");
        Item i5=new Item(3,"x5");
        TreeSet<Item> t=new TreeSet<Item>();    
        t.add(i1);
        t.add(i2);
        t.add(i3);
        t.add(i4);
        t.add(i5);
        System.out.println(t);      
    }
}
class Item implements Comparable<Item>
{
    String  nm;
    int price;
    public Item(int n,String nm)
    {
        this.nm=nm;
        price=n;
    }
    public int compareTo(Item i1)
    {
        // Objects equal so no need to add 
        if(price==i1.price)
        {
            return 0;
        }
        // Object are greater
        else if(price>i1.price)
        {

            return 1;
        }
        // Object is lower
        else
        {
            return -1;
        }
    }
    public String  toString()
    {
        return "\nPrice "+price+" Name : "+nm;
    }    
}