在网上看有关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对象新生成了一个数组。