【Java類集】_IdentityHashMap類筆記

來源:互聯網
上載者:User

【Java類集】_IdentityHashMap類筆記

在正常的Map操作,key本身是不能夠重複的。

import java.util.Map;import java.util.HashMap;import java.util.Iterator;import java.util.Set;class Person{    private String name;    private int age;    public Person(String name,int age){        this.name = name;        this.age = age;        }    public String toString(){        return "姓名:"+this.name+";年齡:"+this.age;        }    public boolean equals(Object obj){        if(this==obj){            return true;        }        if(!(obj instanceof Person)){            return false;        }        Person per = (Person) obj;        if(this.name.equals(per.name)&&this.age==per.age){            return true;        }else{            return false;        }    }    public int hashCode(){        return this.name.hashCode()*this.age;    }}public class HashMapDemo08{    public static void main(String args[]){        Map<Person,String> map = null;        map = new HashMap<Person,String>();        map.put(new Person("張三",30),"zhangsan_1");//增加內容        map.put(new Person("張三",30),"zhangsan_2");//增加內容        map.put(new Person("李四",31),"lisi");//增加內容        Set<Map.Entry<Person,String>> allSet = null;        allSet = map.entrySet();        Iterator<Map.Entry<Person,String>> iter=null;        iter = allSet.iterator();        while(iter.hasNext()){            Map.Entry<Person,String> me = iter.next();            System.out.println(me.getKey()+"-->"+me.getValue());                }    }}

輸出:
姓名:李四;年齡:31-->lisi
姓名:張三;年齡:30-->zhangsan_2

使用HashMap操作的時候,key 內容是不能重複的,如果現在希望key內容可以重複(指的重複是指兩個對象的地址不一樣key1==key2)則要使用IdentityHashMap類。

import java.util.Map;import java.util.IdentityHashMap;import java.util.Iterator;import java.util.Set;class Person{    private String name;    private int age;    public Person(String name,int age){        this.name = name;        this.age = age;        }    public String toString(){        return "姓名:"+this.name+";年齡:"+this.age;        }    public boolean equals(Object obj){        if(this==obj){            return true;        }        if(!(obj instanceof Person)){            return false;        }        Person per = (Person) obj;        if(this.name.equals(per.name)&&this.age==per.age){            return true;        }else{            return false;        }    }    public int hashCode(){        return this.name.hashCode()*this.age;    }}public class IdentityHashMapDemo01{    public static void main(String args[]){        Map<Person,String> map = null;        map = new IdentityHashMap<Person,String>();        map.put(new Person("張三",30),"zhangsan_1");//增加內容        map.put(new Person("張三",30),"zhangsan_2");//增加內容        map.put(new Person("李四",31),"lisi");//增加內容        Set<Map.Entry<Person,String>> allSet = null;        allSet = map.entrySet();        Iterator<Map.Entry<Person,String>> iter=null;        iter = allSet.iterator();        while(iter.hasNext()){            Map.Entry<Person,String> me = iter.next();            System.out.println(me.getKey()+"-->"+me.getValue());                }    }}

輸出:
姓名:張三;年齡:30-->zhangsan_1
姓名:張三;年齡:30-->zhangsan_2
姓名:李四;年齡:31-->lisi

就算是兩個對象的內容相等,但是因為都使用了new 關鍵字,所以地址肯定不等,那麼就可以加入進去,key是可以重複的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.