简体   繁体   中英

Using an interface type with compareTo() - error (java)

Here is my Item class, which implements Thing (my interface)

package moving.domain;

public class Item implements Thing, Comparable<Thing>{

private int volume;
private String name;

public Item (String name, int vol) {

    this.name = name;
    this.volume = vol;
}

public String getName() {

    return this.name;
}

@Override
public int getVolume() {

    return this.volume;
}

 @Override
public String toString() {

    return this.getName() + " (" + this.getVolume() + " dm^3)";
}

@Override
public int compareTo(Thing t) {
    return (int) Integer.compare(this.getVolume(),(t.getVolume())); //To change body of generated methods, choose Tools | Templates.
}

Here is the interface

package moving.domain;

public interface Thing {

    int getVolume();               

}    

and here is the main method

package moving;

import java.util.Collections;
import java.util.List;
import moving.domain.Item;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import moving.domain.Thing;

public class Main {

    public static void main(String[] args) {
        // test your program here
        Map<String, Thing> items2 = new HashMap<String, Thing>();
        //storing interface type Thing in hashmap as value      

        items2.put("three", new Item("passport", 2));
        items2.put("two", new Item("toothbrash", 1));
        items2.put("one", new Item("circular saw", 100));
        List<Thing> items3 = new ArrayList<Thing>(items2.values());

        Collections.sort(items3);
        //
        System.out.println(items3);

    }
}

When I try to sort the collection I get the following error:

no suitable method found for sort(List<Thing>)

method Collections <T#1>sort<List<T#1>) is not applicable

(inferred type does not conform to upper bounds(s)

inferred: Thing

upper bounds(s) Comparable <? super Thing>)

method Collections<T#2>sort(List<T#2>,Comparator<?superT#2>)is not applicable

(cannot infer type - variables T#2

(actual and formal argument lengths differ in length)

where T#1,T#2 are type variables:

T#1 extends comparable <?super T#1> declared in method <T#1>sort(List<T#1>)

t#2 extends object declared in method <t#2>sort(List<t#2>, Comparator<?super t#2>)

I got the same error when I tried to create the same program as shown in section 45 of the following: https://materiaalit.github.io/2013-oo-programming/part2/week-9/

I then recreated the same situation in the above code to see if it happened again and it did. It's really bugging me and I can't seem to understand what the problem is. In the end I even copied and pasted all of the code from section 45 and it still gave the same error, and it gives it here. Can interface types be sorted using an interface reference? Am I missing something obvious? If I change all the type declarations to Item it works.

public class Main {

public static void main(String[] args) {
    // test your program here
      List<Thing> items = new ArrayList<Thing>();
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);

}

I get the same error when I create an ArrayList of things, as above.

Your error occurs because Thing s are not necessarily Comparable . The only class you've made implements both Thing and Comparable<Thing> , but you could easily create:

public class UncomparableThing implements Thing {  /* ... */ }

It's not Comparable . It illustrates the possibility that the compiler sees when you attempt to sort a List<Thing> . Thing s aren't necessarily Comparable .

You could change the list to a List<Item> if you want, because you've defined Item s to be Comparable . Alternatively, you could have the Thing interface extend Comparable<Thing> . Another option is to pass a Comparator<Thing> as a second argument to Collections.sort , as a way to compare objects that aren't Comparable .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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