Method not applicable for generic method arguments

If I change ?

to Object

compile the code.
Q1. Is there a way to change the method signature unwind

for <?> getList()

?
Q2. If not, have you heard of any design principles for designing parameterized APIs with <?>

?

public class Main {
    public static void main(String[] args) {
        unwind(getList());
    }

    public static List<List<?>> getList() {
        return new LinkedList<List<?>>();
    }

    public static<T> Collection<T> unwind(Collection<? extends Collection<T>> collection) {
        return collection.iterator().next();
    }
}

      

PS. I am stuck with handling the results of multiple calls for Future<?> java.util.concurrent.ExecutorService.submit(Runnable task)


me it would be much better to get the result of the Future<Object>

method. Is there a problem with the design of the API here?

+3


source to share


2 answers


I'm still not sure what it is. With generics, you have to be very type specific. You say you are trying to understand the pros and cons. The code doesn't work like this, at least not this code. It all depends on what you are trying to do.

One possible solution is to abuse the trait @SuppressWarnings("unchecked")

  List<List<Future<?>>> llist = getSomething( x );      
  @SuppressWarnings( "unchecked" )
  Collection<?> cx = llist;

      



The other is just that your generic types match 100%. But I still don't know what you are actually trying to do. This is it?

   public static List<List<Future<?>>> getSomething( Future<?> x ) {
      List<List<Future<?>>> llist = new ArrayList<>();
      List<Future<?>> list = new ArrayList<>();
      list.add( x );
      llist.add( list );
      return llist;
   }

   public static <t> Collection<t> unwind( Collection<? extends Collection<t>> c ) {
      return c.iterator().next();
   }

   public static void main(String[] args) {
      Future<?> x = null;
      List<List<Future<?>>> llist = getSomething( x );
      Collection<Future<?>> clist = unwind( llist );
   }

      

+1


source


The wildcard restricts the way geenrics is used.It says it can take any type of .Again as it is any type, you may not be able to add objects, kind of create them read only

So

List<?> list = new ArrayList();
list.add("String"); // compile time error,it is read only

      

The only use of a wildcard type without explicitly including it in another generic type is to use it as a read-only type (reading collection objects since every type is an object)



public void method1(List<?> list){

Object obj = list.get(0);

}

      

Since yours is occupying , list of list of unknown type is not valid for Type T unwind method

Collection of Collection of type T(a specific Type)

I think that you will need the result from unwind()

method (Collection of type T)

later in the program, and therefore you should make a general way getList()

and use it later in the program.

public class Main {
    public static void main(String[] args) {

        Collection<String> val = Main.unwind(Main.<String>getList());

       }

    public static <T> List<List<T>> getList() {
        return new LinkedList<List<T>>();
    }

    public static<T> Collection<T> unwind(Collection<? extends Collection<T>> collection) {
        return collection.iterator().next();
    }
}

      

+1


source







All Articles