0

I have a method for sorting a List of composite Entry objects with Integer key and any type for the value. It has the following signature:

static void bucketSort(List<Entry<Integer, ?>> list, int n)
{
    //some code
}

Where the entry interface is as follows:

interface Entry<K, V> {
    K getKey(); //returns key stored in this entry
    V getValue(); //returns the value stored in this entry
}

However, even though, in my understanding any object can be passed for the "?", this code generates a compile error ("cannot resolve method") in the second line:

List<Entry<Integer, String>> list = new ArrayList<>();
bucketSort(list, 100);

Also, this code gives no error:

List<Entry<Integer, ?>> list = new ArrayList<>();
bucketSort(list, 100);

Can anyone please help me understand why this is? And also, what is the recommended way to resolve the issue?

Stan Ostrovskii
  • 528
  • 5
  • 19

1 Answers1

0

Use wildcard for Entry object also, wildcard represents an unknown type. In the above code you defined Entry<Integer, ?> entry with key Integer and value unknown type, But in the same way you have to say for entry objects in List

static void bucketSort(List<? extends Entry<Integer, ?>> list, int n)
{
    //some code
}
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98