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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 一步步 中级黑马   /  2013-11-14 13:47  /  980 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package twenty_one;
import java.io.*;
import java.util.*;
public class Nine {
        public static void main(String[] args) throws IOException{
        Set<Student>stus=StudentInfoTool.getStudent();
        StudentInfoTool.wrToFile(stus);

        }
}
class Student implements Comparable<Student>
{
private String name;
private int ma,cn,en;
private int sum;
Student(String name,int ma,int cn,int en)
{
        this.name=name;
        this.ma=ma;
        this.cn=cn;
        this.en=en;
        sum=ma+cn+en;
}
public int compareTo(Student s)
{
int num=new Integer(this.sum).compareTo(new Integer(s.sum));
if(num==0)
        return  this.name.compareTo(s.name );
return num;
}
public String getName()
{
return name;       
}
public int getSum()
{
return sum;       
}
public int hashCode()                      &&&&&&&&&&&&&&&&&&&&&&
{
return name.hashCode()+sum*78;       
}
public boolean equals(Object obj)
{
        if(!(obj instanceof Student))
                throw new ClassCastException("类型不匹配");
        Student s=(Student)obj;
        return this.name.equals(s.name)&& this.sum==s.sum;
}
public String toString()
{
return "Student["+name+","+ma+","+","+cn+","+en+"]";       
}
}
class StudentInfoTool
{
        public static Set<Student>getStudent()throws IOException
        {
                BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
                String line=null;
                Set<Student>stus=new TreeSet<Student>();
                while((line=bufr.readLine())!=null)
                {
                        if("over".equals(line))
                                break;
                        String[] info=line.split(",");
                        Student stu=new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]));
                        stus.add(stu);
                }
                bufr.close();
                return stus;
               
        }
        public static void wrToFile(Set<Student>stus)throws IOException
        {
                BufferedWriter bufw=new BufferedWriter(new FileWriter("D://java//workspace//heima//src//twenty_one//File//stuinfo.txt"));
        for(Student stu:stus)
        {//  bufw.write(stu.getName());//只出现姓名与总成绩。
                bufw.write(stu.toString()+"\t");
                bufw.write(stu.getSum()+"");
                bufw.newLine();
                bufw.flush();
        }
        bufw.close();
        }
}


这段程序中
public int hashCode(){return name.hashCode()+sum*78;        }这段代码起什么作业用,我把它删了一点都不影响结果啊?

评分

参与人数 1技术分 +1 收起 理由
黄炳期 + 1

查看全部评分

5 个回复

倒序浏览
这段代码就是返回 String name 的哈希值 再加上sum*78 , 没有其他的功能
回复 使用道具 举报
当你覆盖equals时,应该先覆盖hashCode,因为如果两个对象相等,他们的哈希值一定相同,但是两个不同的对象也可能具有相同的哈希值,就是为了避免出现这种情况

评分

参与人数 1技术分 +1 收起 理由
黄炳期 + 1

查看全部评分

回复 使用道具 举报
哈希值中的值指的是什么
回复 使用道具 举报
FFF 金牌黑马 2013-11-14 16:21:04
报纸
一步步 发表于 2013-11-14 15:51
哈希值中的值指的是什么

用哈希算法得到的一个特征值,理论上每个数据的哈希值都不一样。所以使用这个算法得到的特征值来判断是否是同一个数据。

点评

+1  发表于 2013-11-14 17:52
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马