Reputation: 13
I had implemented a method that accepts a Collections of any type as an input. Is there a better solution?
I'm using generics to solve this problem;
My solution uses this as a parameter (Collection<T> list)
I'm not sure if there is a better option. Is there a better option?, is my solution a good option?
This is my code:
public static <T> boolean doTask(Collection<T> list){
if(list == null)
return false;
else if(list.isEmpty()) return false;
/* more code*/
return true;
}
Upvotes: 0
Views: 202
Reputation: 14999
Since you don't care about the elements' type at all, you can just use ?
for type.
static boolean containsElements(Collection<?> list) {
return list != null && !list.isEmpty();
}
list.isEmpty()
will not be evaluated when list
is null
because the term is already false, so it won't run into an exception.
Upvotes: 2