I am building some generic codes which returning a collection of any type of object. Here is my build:
public class Util{
public static <T, R extends Collection> R a(Collection<T> collection) {
return (R) collection;
}
public static <T> List<T> listWith(T... items) {
return Arrays.asList(items);
}
public static <T> Set<T> setWith(T... items) {
return new HashSet<>(Arrays.asList(items));
}
}
My Idea is from this Util class I can create any list or set of any object type, something like:
List<String> list = a(listWith("str1", "str2"));
Set<Object> set = a(setWith(new Object(), new Object()));
I have now two questions:
- How can I restrict the type of the Obj in the left to be the same as the Obj type of the parameter passed to the
listWithmethod? Because right now I can do something like :Set<String> set = a(setWith(new Object(), new Object()));without any compile error - How can I restrict the return type of the
amethod to be only class that extends the Collection. Right now I can do something like:User user = a(listWith("test"));also without compile error but then it will throw an casting exception. I did try above with return type asR extends Collectionbut it did not success