Results 1 to 5 of 5
Thread: final HashMap hm=new HashMap();
- 07-21-2011, 07:37 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
- 07-21-2011, 09:15 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The reference is final, not the underlying storage. Making the hashmap final doesn't make it unmodifiable, instead it makes it so you can't re-assign the variable to a new hashmap. If you did
In the show method you would run into problems.Java Code:hm = new HashMap();
The hashmap itself is a mutable object; the reference to it is final.
There are methods on the map class to get an unmodifiable view of the hashmap, check the API.
Jos beat me to it while editing my post to add the apiLast edited by sunde887; 07-21-2011 at 09:34 PM.
- 07-21-2011, 09:20 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Also, read the API documentation for the Collections utility class if you want an unmodifiable Map.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-21-2011, 09:31 PM #4
Interesting concept: unmodifiable Map.
Or is that after putting something in it?
- 07-21-2011, 09:44 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
HashMap help
By BobswYourUcle in forum New To JavaReplies: 4Last Post: 05-06-2011, 09:05 PM -
Should i use a hashmap?
By 6Sloth9 in forum New To JavaReplies: 7Last Post: 05-02-2011, 02:38 AM -
Something like HashMap
By BigBear in forum New To JavaReplies: 4Last Post: 03-14-2010, 11:08 AM -
How to create a new HashMap from a HashMap entries of other methods
By pandeyalok in forum Advanced JavaReplies: 7Last Post: 12-08-2009, 07:17 PM -
HashMap Help
By ScaryJello in forum New To JavaReplies: 2Last Post: 04-15-2009, 09:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks