wood burning stoves*
The moose likes Java in General and the fly likes how to use generic in this case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to use generic in this case" Watch "how to use generic in this case" New topic

how to use generic in this case

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
I want to create a class like this

public class Test {
private List<Integer> l1;
private List<Integer> l2;
private List<String> s1;
private List<String> s2;

Test(List<Integer> l1, List<Integer> l2) {
this.l1 = l1; this.l2=l2;

Test(List<String> s1, List<String> s2) {
this.s1 = s1; this.s2 = s2;

So in another code, I can initiate "test" instances by either passing two arguments (each is of type List<Integer> , or passing two arguments (each is of type List<String> .

but I still feel this is bit like too much "hardcoded". Is there any way to improve this to make it more elegant by taking advantage of jdk generic features ? Since I am new to generic it will be very helpful if you ive few code snippet so I know what you are talking about.

Thanks !
Jeff Rummings
Ranch Hand

Joined: Dec 20, 2007
Posts: 42
How about this?

If you need to have two types, change the class definition to
I agree. Here's the link: http://aspose.com/file-tools
subject: how to use generic in this case
Similar Threads
Mapping DB columns to Object
Sorting ArrayList
Important Java Questions
Mapping DB columns to Objects
sorting arraylist of arraylist