Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

method is not applicable for generic method arguments

If I change ? to Object code compiles.
Q1. Is there any way of changing unwind method signature to be applicable for the <?> getList()?
Q2. If not, have you heard any design principles about designing parametrized API 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've stuck on dealing with collection of results of several call to Future<?> java.util.concurrent.ExecutorService.submit(Runnable task)
It would be much better for me to have Future<Object> result of the method. Is there any issue with API design here?

like image 549
Mike Avatar asked Aug 29 '26 11:08

Mike


1 Answers

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

One possible solution is to abuse the heck out of @SuppressWarnings("unchecked")

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

Another is just to make certain your generic types match up 100%. But I still don't know what you are actually trying to do. Is this 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 );
   }
like image 68
markspace Avatar answered Sep 01 '26 02:09

markspace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!