java HashMap中出現重複的key, 求解釋,javahashmap

來源:互聯網
上載者:User

java HashMap中出現重複的key, 求解釋,javahashmap


上代碼:

        Person p1 = new Person("xiaoer",1);        Person p2 = new Person("san",4);        Map<Person,String> maps = new HashMap<Person,String>();        maps.put(p1, "1111");        maps.put(p2, "2222");        System.out.println(maps);        maps.put(p2, "333");        System.out.println(maps);        p1.setAge(5);        System.out.println(maps);        maps.put(p1, "333");        System.out.println(maps);        System.out.println(maps.get(p1));

輸出結果:

{Person [name=san, age=4]=2222, Person [name=xiaoer, age=1]=1111}
{Person [name=san, age=4]=333, Person [name=xiaoer, age=1]=1111}
{Person [name=san, age=4]=333, Person [name=xiaoer, age=5]=1111}
{Person [name=san, age=4]=333, Person [name=xiaoer, age=5]=1111, Person [name=xiaoer, age=5]=333}
333


重點關注 紅色的 標註。


其中Person類如下:

class Person{    private String name;    private int age;    @Override    public int hashCode() {        final int prime = 31;        int result = 1;        result = prime * result + age;        result = prime * result + ((name == null) ? 0 : name.hashCode());        return result;    }    /* (non-Javadoc)     * @see java.lang.Object#equals(java.lang.Object)     */    @Override    public boolean equals(Object obj) {        if (this == obj)            return true;        if (obj == null)            return false;        if (getClass() != obj.getClass())            return false;        Person other = (Person) obj;        if (age != other.age)            return false;        if (name == null) {            if (other.name != null)                return false;        } else if (!name.equals(other.name))            return false;        return true;    }    /* (non-Javadoc)     * @see java.lang.Object#toString()     */    @Override    public String toString() {        return "Person [name=" + name + ", age=" + age + "]";    }}




why?   輸出的maps 出現了相同的key。


初步 判斷 和  HashMap的 hashcode機制有關, 只在第一次 將 元素  add 加入map時,檢測元素key的 hash值。 之後我通過外部 手段 更改了對象的值,再將 該對象加入 map,其實 從hashcode來看 已經是一個新的 對象了,故 map認為他們的key 不同。HashMap為了提高校正速度,並不會 將待增的元素 與 map中已有的所有元素 一 一 比較,而只是快速的比較hashcode table 表?    雖然從實體記憶體上看 他們的確是 同一個對象。


java 這樣設計的原理 何在?


這樣其實存在不安全性。 譬如 我將一個 hashMap對象 傳給了 方法A 去處理,結果A還在處理中的 時候,我把hashMap對象的裡面的一個元素的值 給 簡介更改了,而A 竟然還不知情。


 與 HashMap的線程不安全 是 幾回事?








java裡 hashmap如果key相同則覆蓋 問是怎判斷相同的?是比較的key的內容還是比較key的地址?詳細說說

是根據key的hashcode值來判斷的
 
合并兩個hashmap,重複的值只顯示一項

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Cat {

public static void main(String[] args) {
Map map1 = new HashMap();

map1.put("1", "1");
map1.put("2", "2");
map1.put("3", "3");
map1.put("4", "4");

Map map2 = new HashMap();
map2.put("3", "3");
map2.put("4", "4");
map2.put("5", "5");
map2.put("6", "6");

Map map3 = new HashMap();
map3.putAll(map1);
map3.putAll(map2);

for (Iterator iter = map3.keySet().iterator(); iter.hasNext();) {
String key = (String) iter.next();
System.out.println(key + "<---> " + map3.get(key));

}

}

}

--------------------------
4<---> 4
5<---> 5
6<---> 6
1<---> 1
2<---> 2
3<---> 3
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.