Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use generic in this case

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this?



If you need to have two types, change the class definition to
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic