1. 程式人生 > >Java IdentityHashMap重複值的輸出問題

Java IdentityHashMap重複值的輸出問題

package org13.example;


import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

class Man{
 private String name;
 private int age;
 public Man(String name,int age){
  this.setName(name);
  this.setAge(age);
 }
 public void setName(String name){
  this.name = name;
 }
 public String getName(){
  return name;
 }
 public void setAge(int age){
  this.age = age;
 }
 public int getAge(){
  return age;
 }
 public boolean equals(Object obj){
  if(this==obj){
   return true;
  }
  if(!(obj instanceof Man)){
   return false;
  }
  Man p = (Man)obj;
  if(this.name.equals(p.name) && this.age==p.age){
   return true;
  }else{
   return false;
  }
 }
 public int hashCode(){
  return this.name.hashCode()*this.age;
 }
 public String toString(){
  return "姓名:"+this.name+"\t年齡:\n"+this.age;
 }
}
public class IdentityHashDemo01 {

 public static void main(String[] args) {
  Map<Man,String> map = null;
  map =new IdentityHashMap<Man,String>();
  map.put(new Man("張三",20),"zhangsan1");
  map.put(new Man("李四",20),"zhangsan2");
  map.put(new Man("張三",20),"zhangsan3");
  Set<Map.Entry<Man,String>> allset =null;
  allset =map.entrySet();
  Iterator<Map.Entry<Man,String>> allIter =null;
  allIter = allset.iterator();
  while(allIter.hasNext()){
   Map.Entry<Man,String> me = allIter.next();
   System.out.println(map.get(me.getKey()+"--->"+me.getValue()));
  }

 }

}

為什麼輸出的都是null?求大神指教