• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

how to use generic in this case

 
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 !
 
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
Boost this thread!