Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private instance variable of Map type

 
Stefanos Madagaskaris
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I initialize in a constructor this private instance variable?

private Map<String, Set<Integer>> variable;

I tried to use :

variable = new HashMap<String, TreeSet<Integer>>();

because I want each set to be ordered but I get an error.
Any ideas guys?
Thank you.
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to keep the bind types the same for the instantiated object:



but when you put elements into the map they can be of a concrete subtype:


If the client needs to know (doubtful in this case), or if you're worried about someone putting in a non-treeset instance, declare the map as binding to String and HashSet<Integer>.

Jeff
[ September 09, 2008: Message edited by: Jeff Langr ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic