0

Java context:

Class: Channel implements ICleanable
Variable: Set<Channel> channels = new HashSet<>()

I need to call this method with an array of Iterable as parameter:

void unexport(Iterable<ICleanable>[] cleanables)

But I cannot devise how to write the call. This version doesn't work:

unexport(new Iterable<ICleanable>[] { channels })

The compiler says: "Type mismatch: cannot convert from Set<Channel> to Iterable<ICleanable>"

Help would be appreciated (and/or reference to learning material). Thanks.


EDIT. Now that I've got the answer, I can summarize where I was wrong.

I had a problem with generics and subtypping. This is a beginner error (which is exactly what I'm). Clue:

List<String> ls = new ArrayList<String>();  
List<Object> lo = ls; // Illegal, a List<String> is not a List<Object>!  
lo.add(new Object());  
String s = ls.get(0); // Attempts to assign an Object to a String!  

A List<String> cannot be a List<Object>, otherwise the type safety void be void. This is explained here.

I had a second problem with array of generics.

unexport(new Iterable<ICleanable>[] { channels })

It is not possible to create an array of Iterable. This is explained here. The error on type conversion prevented this second error to be detected by the compiler before correction of the former.

In my case I ended up with this:

void unexport(List<List<? extends ICleanable>> cleanables) {...}
Channel implements ICleanable;
List<Channel> channels = new ArrayList<>();
List<List<? extends ITVCleanable>> list = new ArrayList<>();
list.add(channels);
unexport(list);
mins
  • 4,503
  • 9
  • 45
  • 62
  • Does `channels.iterator();` work? Hang on, why is it an `Iterable[]`? – doctorlove Aug 02 '14 at 14:07
  • 1
    You're also trying to create generic array there. – August Aug 02 '14 at 14:11
  • @doctorlove. With you suggestion: `cannot convert from Iterator to Iterable`. In the method I want to process a list of unknown size of (Set of Iterable). I have though of using ... but it creates a heap pollution door, so the array. Channel actually extends UnicastRemoteObject and implements ICleanable. The method is interested only by the ICleanable features. – mins Aug 02 '14 at 14:16
  • Generics = parameterized types + parameterized methods + wildcards. Easy to understand concepts. but generics do not seem to fit well with the rest of Java: numerous limitations, exceptions and special cases lead to a big spaghetti plate. Those cases make little sense, or are not consistent, hence are difficult to remember. A short + comprehensive presentation of these cases, and the rational of their existence would be superior to multiple incomplete explanations about a specific case. Does someone knows about such summary? – mins Aug 03 '14 at 11:37

1 Answers1

1

You're trying to create a generic array at new Iterable<ICleanable>[]. You can't create a generic array directly, so instead you can do this:

unexport((Iterable<ICleanable>[]) new Iterable[] { channels });
Community
  • 1
  • 1
August
  • 10,796
  • 2
  • 30
  • 47