A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

package jihe01;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MapDemo4 {

        public static void main(String args[]){
                method();
        }
        public static void method(){
                HashMap<String ,List<student2>> csdn=new HashMap<String, List<student2>>();
                List<student2> yure=new ArrayList<student2>();
                List<student2> jiuye=new ArrayList<student2>();
                csdn.put("yure",yure);
                csdn.put("jiuye",jiuye);
                yure.add(new student2("01","zhangsan"));
                yure.add(new student2("02","lisi"));
                yure.add(new student2("01","zhangsan"));
                jiuye.add(new student2("01","wangwu"));
                jiuye.add(new student2("02","zhaoliu"));
                Iterator<String > it=csdn.keySet().iterator();
                while(it.hasNext()){
                        String roomName=it.next();
                        List<student2> room=csdn.get(roomName);
                        sop(roomName);
                        getStu(room);
                       
                }
        }
        public static void getStu(List<student2> room){
                Iterator<student2> it=room.iterator();
                while(it.hasNext()){
                        sop(it.next());
                }
        }
        public static void sop(Object obj){
                System.out.println(obj);
        }
}
class student2 implements Comparable<student2>{
        private String id;
        private String name;
        public student2(String id,String name){
                this.id=id;
                this.name=name;
        }
        public String toString(){
                return id+"..."+name;
        }
        public int compareTo(student2 student){
                int num=new Integer(id.hashCode()).compareTo(new Integer(student.id.hashCode()));
                if(num==0)
                        return name.compareTo(student.name);
                return num;
        }
        public int hashCode(){
                return name.hashCode()+id.hashCode();
        }
        public boolean equals(Object obj){
                if(!(obj instanceof student2))
                        throw new ClassCastException("类型不匹配");
                student2 student=(student2)obj;
                return name.equals(student.name) && id.equals(student.id);
        }
}
为什么还能将重复元素添加进来


捕获.JPG (29.91 KB, 下载次数: 4)

捕获.JPG

点评

你好好看看视频,绝对不是这么写的。  发表于 2015-3-10 11:52

3 个回复

倒序浏览
我知道不是这么写的,老师在写这段程序时,并没有在student类中复写hashCode和equals方法,也没有实现comparable接口,我就是想知道这样做能不能保证集合中数据的唯一性
回复 使用道具 举报
本帖最后由 z47057554 于 2015-3-10 13:20 编辑

      你把元素的存储关系弄混了,你的student2是存储于ArrayLiat中的,Map管不到它里面的ArrayList是不是有重复元素的。
      Map能管的是<String ,List<student2>> 中的String,和 List<student2>>(看好,是List,不是student2),
      换句话说,你的student2是存储于ArrayList中的,而student2中的hashCode和equals方法,实现comparable接口对ArrayList来说是没有用的,
      至于你把ArrayList存于Map中,Map看的是ArrayList的hashCode和equals方法,
      另外说下,存放于HashSet和HashMap(以Hash开头的容器)时用到的是hashCode和equals方法,存放于TreeSet和TreeMap(以Tree开头的容器)时用到的是实现comparable接口,当然全部写上最好

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马