aspose file tools
The moose likes Java in General and the fly likes Generic Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Generic Methods" Watch "Generic Methods" New topic
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
    
  10

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Generic Methods
 
Similar Threads
Cross Reference two ArrayList Duplicates Removal
generics compiler warning
good coding practice?
Transfering 3rd party (unserialized) objects through web server.
transfer unserilaized objects as SOAP response