File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


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
Author

how to use generic in this case

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
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
 
GeeCON Prague 2014
 
subject: how to use generic in this case