------------------------------没用泛型前--------------------------
public class MapTest2 {
public static void main(String[] args) {
Map computer = new HashMap();
Computer c1 = new Computer("华硕",3200.0);
Computer c2 = new Computer("联想",3100.0);
Computer c3 = new Computer("Mac",6700.0);
Computer c4 = new Computer("方正",3200.0);
Computer c5 = new Computer("惠普",4200.0);
Computer c6 = new Computer("惠普",4200.0);
computer.put(c1, 15);
computer.put(c2, 14);
computer.put(c3, 17);
computer.put(c4, 19);
computer.put(c6, 12);
computer.put(c5, 12); //key值重复自动覆盖
System.out.println(computer.size());
computer.containsKey("联想");
computer.containsValue(12);
computer.remove("方正");
computer.get("Mac");
Collection numbers = computer.values();
Iterator numberIt = numbers.iterator();
while(numberIt.hasNext()){
Integer number = (Integer) numberIt.next();
System.out.println(number);
}
//Set和Iterator必须指明存储类型否则编译报错
Set computers = computer.keySet();
Iterator computersIt = computers.iterator();
while(computersIt.hasNext()){
//类型不统一的话则需要,使用Object,instanceof分情况
Computer c = (Computer) computersIt.next();
System.out.println(c.name+","+c.price+"元,"+computer.get(c)+"台");
}


}}class Computer{
String name;
double price;
Computer(String name, double price){
this.name = name;
this.price = price;
}
public String toString(){
return "name = "+ name+" ,price = " + price;
}
//HashMap中必须重写hashCode和equals方法否则会出现重复元素
//首先比较hashCode若不等,直接存入
//若想等,再比较equals,若不等,则放弃
public int hashCode(){
return name.hashCode()*(price+"").hashCode();
}
  public boolean equals(Object o){
if(this == o){return true;}
if(o instanceof Computer){
Computer c = (Computer)o;
if(c.name.equals(name) && c.price==price){
return true;
}
}
return false;
}
}
=====
5
15
19
14
12
17
华硕,3200.0元,15台
方正,3200.0元,19台
联想,3100.0元,14台
惠普,4200.0元,12台
Mac,6700.0元,17台
------------------------------用了泛型后--------------------------
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;public class MapTest2 {
public static void main(String[] args) {
Map<Computer,Integer> computer = new IdentityHashMap<Computer,Integer>();
Computer c1 = new Computer("华硕",3200.0);
Computer c2 = new Computer("联想",3100.0);
Computer c3 = new Computer("Mac",6700.0);
Computer c4 = new Computer("方正",3200.0);
Computer c5 = new Computer("惠普",4200.0);
Computer c6 = new Computer("惠普",4200.0);
computer.put(c1, 15);
computer.put(c2, 14);
computer.put(c3, 17);
computer.put(c4, 19);
computer.put(c6, 12);
computer.put(c5, 12); //key值重复自动覆盖
System.out.println(computer.size());
computer.containsKey("联想");
computer.containsValue(12);
computer.remove("方正");
computer.get("Mac");
Collection<Integer> numbers = computer.values();
Iterator<Integer> numberIt = numbers.iterator();
while(numberIt.hasNext()){
Integer number = numberIt.next();
System.out.println(number);
}
//Set和Iterator必须指明存储类型否则编译报错
Set<Computer> computers = computer.keySet();
Iterator<Computer> computersIt = computers.iterator();
while(computersIt.hasNext()){
//类型不统一的话则需要,使用Object,instanceof分情况
Computer c = computersIt.next();
System.out.println(c.name+","+c.price+"元,"+computer.get(c)+"台");
}


}}class Computer{
String name;
double price;
Computer(String name, double price){
this.name = name;
this.price = price;
}
public String toString(){
return "name = "+ name+" ,price = " + price;
}
//HashMap中必须重写hashCode和equals方法否则会出现重复元素
//首先比较hashCode若不等,直接存入
//若想等,再比较equals,若不等,则放弃
public int hashCode(){
return name.hashCode()*(price+"").hashCode();
}
  public boolean equals(Computer c){
if(this == c){return true;}
if(c.name.equals(name) && c.price==price){
return true;
}
return false;
}
}
==========
6
12
12
15
17
14
19
惠普,4200.0元,12台
惠普,4200.0元,12台
华硕,3200.0元,15台
Mac,6700.0元,17台
联想,3100.0元,14台
方正,3200.0元,19台
======================
"惠普,4200.0元,12台",明明过滤掉了,怎么还是有重复的key?请大神赐教!!!

解决方案 »

  1.   

    Map computer = new HashMap();
    Computer c1 = new Computer("华硕",3200.0);
    Computer c2 = new Computer("联想",3100.0);
    Computer c3 = new Computer("Mac",6700.0);
    Computer c4 = new Computer("方正",3200.0);
    Computer c5 = new Computer("惠普",4200.0);
    Computer c6 = new Computer("惠普",4200.0);
    computer.put(c1, 15);
    computer.put(c2, 14);
    computer.put(c3, 17);
    computer.put(c4, 19);
    computer.put(c6, 12);
    computer.put(c5, 12);//key值重复自动覆盖
    System.out.println(computer.size());结果为6,为什么你的结果为5?
      

  2.   

    代码好乱 不太想看..  你贴到格式里面就好了.. 我用第一块的代码跑过之后   
    //        Map computer = new HashMap();
            Map<Computer,Integer> computer = new HashMap<Computer,Integer>();得到的两个结果都是5 不知道你下面的代码有什么区别
      

  3.   

    问题解决了,是equals方法,方法重写,
    pubic boolean equals(Object o),我把Object o写成了Computer c。