即使声明了可比较的Java Collections.sort()也无法正常工作

问题描述 投票:0回答:6

这是我的界面类

public interface Thing {
    int getVolume();
}

这是实现Thing的类

item.Java

public class Item implements Thing, Comparable<Thing> {
    private String name;
    private int volume;

    public Item(String name,int volume){
        this.name = name;
        this.volume = volume;
    }

    @Override
    public int getVolume() {
        return this.volume;
    }

    public String getName(){
        return this.name;
    }

    @Override
    public String toString(){
        return name+" ("+volume+" dm^3)";
    }

 //   @Override
    @Override
    public int compareTo(Thing another) {
        if(this.getVolume()  < another.getVolume()){
            return -1;
        }

        if(this.getVolume() == another.getVolume()){
            return 0;
        }
        else{
            return 1;
        }
    }

}

当我尝试使用以下命令运行主程序时,它运行正常// main program.java

public class Main {

    public static void main(String[] args) {
        // test your program here
     List<Item> items = new ArrayList<Item>();
    items.add(new Item("passport", 2));
    items.add(new Item("toothbrash", 1));
    items.add(new Item("circular saw", 100));

    Collections.sort(items);
    System.out.println(items);



    }
}

但是当我尝试在另一个实现Thing接口的类上运行Collections.sort()时,我收到一个错误

这是实现Thing接口的box类,当我尝试在void sort()函数中运行Collections.sort(store)时,即使store是List而Box类实现了Thing接口,我也有错误在Item.java类中为Thing定义了可比较的

box.Java

public class Box implements Thing {

    private int maximumCapacity;
    private List<Thing> store;

    public Box(int maximumCapacity) {
        this.maximumCapacity = maximumCapacity;
        this.store = new ArrayList<Thing>();
    }

    public boolean addThing(Thing thing) {
        // I.E. if the item added does not make the total volume go to max capacity only
        // then add
        if (this.getVolume() + thing.getVolume() < this.maximumCapacity) {
            store.add(thing);
            return true;
        }
        return false;
    }

    @Override
    public int getVolume() {
        // we calculate things of all items in the boxes (current value)
        int currentWeight = 0;
        for (Thing t : store) {
            currentWeight += t.getVolume();
        }
        return currentWeight;
    }

    public List<Thing> getStore() {
        return store;
    }

    public int numOfItems(){
        return this.store.size();
    }


     public void sort(){ 

        Collections.sort(store); // *****does not work ****//

     }

}

它给出了一个错误,排序为“找不到合适的排序方法(List <Thing>)”。

我的问题是,它是否可以在main.java程序中工作,其中项目以List的形式给出,为什么它不能在这里工作?怎么解决?

java sorting comparable
6个回答
3
投票

它排序的主要类List<Item> Item implements Thing, Comparable<Thing>

Box类中,您尝试对List<Thing>进行排序,但Thing本身并未实现Comparable<Thing>。因此Java不知道如何对Things进行排序。

要解决这个问题,你要么必须为两个Things提供一个比较器(由АлександрНестеров提出),或者你声明Thing implements Comparable<Thing>

public interface Thing extends Comparable<Thing>{

    int getVolume();

    //provide default method to sort any class which implements Thing
    @Override
    public default int compareTo(Thing another) {
        return Integer.compare(this.getVolume(), another.getVolume());
    }
}

2
投票

这是因为首先你排序“item”,然后你排序“list thing” 所以,你可以使用lambda修复它:

Collections.sort(store, (o1, o2) -> {
your implementation of comparator
});

0
投票

我建议您定义Thing以扩展Comparable,因为当您添加不是Comparable的类时,您的应用程序不起作用。

顺便说一句,你的compareTo看起来相当复杂。改为:

int compareTo(Thing another) {
    return this.getVolume()  - another.getVolume();
    }

0
投票

在第一个程序中,你有

public class Item implements Thing, Comparable<Thing>

但在第二个,你就是

public class Box implements Thing

如果你想要排序工作,你需要实现Comparable或Comparator(单独实现Comparator的类)。


0
投票

如果您使用的是jdk 8或更高版本,并且希望所有实现“Thing”的类都应该在相同参数的基础上进行排序,您应该将接口更改为此以提供默认排序功能:

//extend your interface with comparable

public interface Thing extends Comparable<Thing>{

int getVolume();

//provide default method to sort any class which implements Thing
@Override
public default int compareTo(Thing another) {
    if(this.getVolume()  < another.getVolume()){
        return -1;
    }

    if(this.getVolume() == another.getVolume()){
        return 0;
    }
    else{
        return 1;
    }
}

}

现在Item和Box只需要实现Thing接口。

请尝试按照@Jo Witters的建议优化compareTo()方法


-1
投票

Make Thing是一个实现Comparable的抽象类,因此Thing总是可以进行排序。物品可以从Thing扩展

© www.soinside.com 2019 - 2024. All rights reserved.