aspose file tools
The moose likes Beginning Java and the fly likes private instance variable of Map type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "private instance variable of Map type" Watch "private instance variable of Map type" New topic
Author

private instance variable of Map type

Stefanos Madagaskaris
Greenhorn

Joined: Sep 09, 2008
Posts: 3
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

Joined: May 14, 2003
Posts: 796
    
    5
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 ]

Books: Pragmatic Unit Testing in Java, Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
 
Have you tried LearnNowOnline? http://www.learnnowonline.com/
 
subject: private instance variable of Map type