3

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?

Mike
  • 20,010
  • 25
  • 97
  • 140
  • So the question is:- You want to pass the result returned by getList() into the argument of unwind()? – Kumar Abhinav Aug 23 '14 at 22:16
  • Yes, show us the bit that doesn't compile. What are you trying to do? – markspace Aug 23 '14 at 22:18
  • I believe iut does not compile since because you're trying to pass a variable of type List> into a method that expects an argument of type Collection extends Collection> (calling "unwind(getList())"). The former is parametrized for a list of unknown type whereas the latter is a parametrized for a collection type T. With this structure, "unwind" method might end up returning a collection of unknown type. Is that what you want? – gramonov Aug 23 '14 at 22:23
  • @Mykhaylo Adamovych Generics has so many solutions.It actually depends on what you are trying to do – Kumar Abhinav Aug 23 '14 at 22:25
  • I'm trying to understand where should I use `>` and where `` in API methods, what the pros and cons, and if there is any 'philosophy' around `>` ussage – Mike Aug 23 '14 at 22:27
  • I tend to use `` where possible, but once I was unable to use `` in my API method because of JRE core type method signature – Mike Aug 23 '14 at 22:29
  • possible duplicate of [When to use wildcards in Java Generics?](http://stackoverflow.com/questions/16707340/when-to-use-wildcards-in-java-generics) – Kumar Abhinav Aug 23 '14 at 22:30

2 Answers2

1

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 );
   }
markspace
  • 10,621
  • 3
  • 25
  • 39
1

Wildcard limits the way you use geenrics.It says it can accept any type.Again,since it is of any type you possibly cannot add Objects,kind of making 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 casting it into another generic type is to use it as a read only type(read the Objects of the Collections since the every type is an Object)

public void method1(List<?> list){

Object obj = list.get(0);

}

Since your unwind method takes a Collection of Collection of type T(a specific Type),List of List of unknown type is not a valid substitute here for Type T

I am thinking that you would need the result returned from unwind() method (Collection of type T) further in the program and therefore you should make the getList() method generic and use it further 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();
    }
}
Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • I would happy to use `` in `getList()`, but frankly speaking this is `ExecutorService.submit()` that limits me. – Mike Aug 23 '14 at 22:51
  • @Mykhaylo Adamovych Wildcard cannot be used where type parameters are required.But you can use legacy code (without types) – Kumar Abhinav Aug 23 '14 at 23:01