---
title: "Java Final关键字与HashMap"
author: "guiven"
date: "2013-12-21T15:21:07Z"
tags: ["Java"]
categories: ["编程语言"]
slug: "java-final-hashmap"
draft: false
lastmod: "2013-12-21T15:21:07Z"
id: 1993305
url: "https://guiven.iteye.com/blog/1993305"
---

# Why final is not applied for Map ?

[]()[Prabhat Ranjan](http://www.coderanch.com/forums/user/profile/135407)   
Ranch Hand 

Joined: Oct 04, 2006  
Posts: 374 

posted 2010年11月16日 GMT+8下午8:00:19 ![purple mooseage](http://cache-www.coderanch.com/templates/default/images/icon_pm.gif "You must login or register")

![](http://cache-www.coderanch.com/templates/default/images/thumbs-up.gif)

![Quote](http://cache-www.coderanch.com/templates/default/images/icon_quote.gif "You must login or register")

Hi, 

Map hm= new [HashMap](http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html "Java API")();   
hm.put("hash","test"); 

ok 

final Map hm= new [HashMap](http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html "Java API")();   
hm.put("hash","test");// this should not allow here as Map i have declared as final. 

ko 

but Map hms = Collections.unmodifiable(hm) is working fine. ![](http://cache-www.coderanch.com/templates/default/images/spacer.gif) []()[Matthew Brown](http://www.coderanch.com/forums/user/profile/225341)   
Bartender 

Joined: Apr 06, 2010  
Posts: 4115   
    

    4

[I like...](http://www.coderanch.com/how-to/java/BumperStickers)  
![Java](http://www.coderanch.com/images/bumperStickers/16_java.gif "Java") ![Netbeans IDE](http://www.coderanch.com/images/bumperStickers/16_netbeans.gif "Netbeans IDE") ![Scala](http://www.coderanch.com/images/bumperStickers/16_scala.png "Scala")

posted 2010年11月16日 GMT+8下午8:02:20 ![purple mooseage](http://cache-www.coderanch.com/templates/default/images/icon_pm.gif "You must login or register")

![](http://cache-www.coderanch.com/templates/default/images/thumbs-up.gif)

![Quote](http://cache-www.coderanch.com/templates/default/images/icon_quote.gif "You must login or register")

> Prabhat Ranjan wrote:final Map hm= new [HashMap](http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html "Java API")();   
> hm.put("hash","test");// this should not allow here as Map i have declared as final.

But `hm` is still pointing at the same object. `final` means that the *reference* can't be altered, not the object itself. ![](http://cache-www.coderanch.com/templates/default/images/spacer.gif) []()[Paul Sturrock](http://www.coderanch.com/forums/user/profile/71044)   
Bartender 

Joined: Apr 14, 2004  
Posts: 10336 

[I like...](http://www.coderanch.com/how-to/java/BumperStickers)  
![Eclipse IDE](http://www.coderanch.com/images/bumperStickers/16_eclipse.gif "Eclipse IDE") ![Hibernate](http://www.coderanch.com/images/bumperStickers/16_hibernate.gif "Hibernate") ![Java](http://www.coderanch.com/images/bumperStickers/16_java.gif "Java")

posted 2010年11月16日 GMT+8下午8:09:12 ![purple mooseage](http://cache-www.coderanch.com/templates/default/images/icon_pm.gif "You must login or register")

![](http://cache-www.coderanch.com/templates/default/images/thumbs-up.gif)

![Quote](http://cache-www.coderanch.com/templates/default/images/icon_quote.gif "You must login or register")

> hm.put("hash","test");// this should not allow here as Map i have declared as final. [

Final does not imply unmodifiable, unless the object is immutable. If you want an unmodifiable map have a look at the Collections class, or alternatively wrap the map in your own custom immutable class.

 

 

如果要实现HashMap不可被修改，即如果不可变类的成员变量是可变的（譬如HashMap），我们需要使用深拷贝(deep copy)或者克隆来防止成员变量被更改：

`private` `final` `HashMap testMap;`

 

……

……

……

 

`/**`

`* 可变对象的访问方法`

`*/`

`public` `HashMap getTestMap() {`

`//return testMap;`

`return` `(HashMap) testMap.clone();`

`}`

` `