| Author |
Generic Methods
|
Flavio Moretti
Greenhorn
Joined: Jun 14, 2010
Posts: 1
|
|
I am writing a method that should return either a List<Long> or a List<String>
my method thus far is such:
item.getValue() returns a String. I'm trying to use generics for type safety.
The problem is we don't know at design time the <T> used to create the List,
so it won't let me add a Long to a List of type <T>. If I can't get this to work,
I'll probably just create two methods that aren't generic, one that returns
List<Long> and the other that returns List<String>.
Any suggestions here would be greatly appreciated.
|
 |
Greg Charles
Bartender
Joined: Oct 01, 2001
Posts: 2536
|
|
|
Yes, make two methods. You could just use the regular List classes, which are perfectly happy to store either Strings or Longs or anything else. However, I'd prefer to have two methods with descriptive names like getNameList() and getValueList() or something else that makes sense for your domain.
|
 |
 |
|
|
subject: Generic Methods
|
|
|