Reputation: 87
Is is possible to have a generic return type such that collection can built with interface as well as implementation.
public static void main(String[] args) {
List<Test1> list = ImmutableList.of(new Test1());
List<ITest> test_return_1 = buildIntRange(list);
List<Test1> test_return_2 = buildIntRange(list); //error
}
private static <K extends ITest> List<ITest> buildIntRange(List<K> test) {
return ImmutableList.copyOf(test);
}
Upvotes: 3
Views: 71
Reputation: 198471
Sure. Use the same signature as ImmutableList.copyOf
, or just use ImmutableList.copyOf
directly:
static <T> List<T> copyOf(Iterable<? extends T> collection)
Upvotes: 4