在網上看有關HashMap的由關文章的時候,別人都說HashMap的clone方法是淺拷貝,但在看了源碼後有所疑問,爲何HashMap的方法是淺拷貝呢?下面是對HashMap的clone方法的一個驗證。java
淺拷貝:對一個對象進行clone生成新的對象,新的對象要開闢一塊新的內存來存儲,新對象中的基本類型屬性和String類型屬性都會開闢新的空間存儲,可是若是是引用類型的屬性,那這個引用類型的屬性仍是指向原對象的引用屬性內存,當對新的對象或原對象的引用屬性作出改變的時候,兩方的引用屬性類型的值同時作出改變。spring
下面是對HAshMap的clone方法的驗證數組
1.先申明一個bean做爲引用類型 app
public class Student {
private String name;
private String sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
public Student(){
}
public Student(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
}
ide
2.調用main方法進行驗證 this
package com.spring.test;
import java.util.HashMap;
import java.util.Hashtable;
import com.spring.bean.Student;
/**
* @author 做者 wangbiao
* @date 建立時間:2016年9月22日 下午4:41:17
* @parameter
* @return
*/
public class MapCloneTest {
public static void main(String[] args) {
Student zhangsan = new Student("zhangsan","男",25);
HashMap<Integer,Object> map = new HashMap<Integer,Object>();
map.put(1, zhangsan);
HashMap<Integer,Object> cloneMap = (HashMap<Integer, Object>) map.clone();
System.out.println("*************************不作改變***********************************");
System.out.println("未改變以前, map的值:"+map.toString());
System.out.println("未改變以前,cloneMap的值:"+cloneMap.toString());
System.out.println("map和cloneMap是否指向同一內存地址:"+(map==cloneMap));
System.out.println("map和cloneMap中存儲的student是否指向同一內存地址:"+(map.get(1)==cloneMap.get(1)));
//對cloneMap中的值進行改變,看是否能影響到map
Student cloneLisi = (Student) cloneMap.get(1);
cloneLisi.setSex("女");
System.out.println("*************************對map中的值作出修改****************************");
System.out.println("改變以後,cloneMap的值:"+cloneMap.toString());
System.out.println("改變以後, map的值:"+map.toString());
System.out.println("*************************對map新增**********************************");
Student lisi = new Student("lisi","男",18);
map.put(2, lisi);
System.out.println("改變以後,cloneMap的值:"+cloneMap.toString());
System.out.println("改變以後, map的值:"+map.toString());
}
}
從HashMap的克隆方法能夠看出這樣的幾點
1.HashMap的clone方法生成新的對象,新的對象有本身獨立的存儲空間。.net
2.雖然HashMap的clone方法生成了新的對象,但新的HashMap和原來的HashMap所存儲的引用類型都是指向的同一存儲空間。對象
3.新生成的HashMap中的數組屬性table是開闢了新的空間的,只是table中的存儲的值所指向的內存空間和原來的是同樣的。這和我理解的淺拷貝的有點不同。我理解的淺拷貝認爲是對象中的引用屬性指向的是同一內存地址,但HashMap中的table數組卻不是指向同一地址,而是table裏面的值指向同一地址。內存
HashMap的clone方法源碼get
/** * Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and * values themselves are not cloned. *key和value不被複制 * @return a shallow copy of this map */ public Object clone() { HashMap<K,V> result = null; try { result = (HashMap<K,V>)super.clone(); } catch (CloneNotSupportedException e) { // assert false; } //新申明瞭一個數組,這說明兩個hashMap的數組不是指向的通一個對象 result.table = new Entry[table.length]; result.entrySet = null; result.modCount = 0; result.size = 0; result.init(); //將原HashMap中存儲的元素複製到新的HashMap裏面 result.putAllForCreate(this); return result; } //遍歷原HashMap private void putAllForCreate(Map<? extends K, ? extends V> m) { for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) putForCreate(e.getKey(), e.getValue()); } //將key-value放入新的數組中 private void putForCreate(K key, V value) { int hash = null == key ? 0 : hash(key); int i = indexFor(hash, table.length); /** * Look for preexisting entry for key. This will never happen for * clone or deserialize. It will only happen for construction if the * input Map is a sorted map whose ordering is inconsistent w/ equals. */ for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) { e.value = value; return; } } createEntry(hash, key, value, i); } void createEntry(int hash, K key, V value, int bucketIndex) { Entry<K,V> e = table[bucketIndex]; //這裏若是value是引用類型,這裏就是引用傳遞 table[bucketIndex] = new Entry<>(hash, key, value, e); size++; } 從源碼中能夠看出clone方法雖然生成了新的HashMap對象,新的HashMap中的table數組雖然也是新生成的,可是數組中的元素仍是引用之前的HashMap中的元素。這就致使在對HashMap中的元素進行修改的時候,即對數組中元素進行修改,會致使原對象和clone對象都發生改變,但進行新增或刪除就不會影響對方,由於這至關因而對數組作出的改變,clone對象新生成了一個數組。