課程
/后端開發(fā)
/Java
/Java入門第三季
同樣的問題,為什么我寫的equals和hashcode方法還是返回false?
2017-04-18
源自:Java入門第三季 6-4
正在回答
錯誤如上圖。
左岸的風(fēng)有咖啡香
public class Maptest {
Scanner input = new Scanner(System.in);
//private static final String ID = null;
private Map<String,Student> Students;
public Maptest(){
this.Students=new HashMap<String,Student>();
}
public void testPut(){
Scanner input=new Scanner(System.in);
int i=1;
while(i<=3){//3個學(xué)生
System.out.print("請輸入第"+i+"學(xué)生ID:");
String ID=input.next();
Student st = Students.get(ID);//保存id,判斷id是否被占用
if(st==null){//如果當(dāng)前id為空
System.out.print("請輸入學(xué)生姓名:");
String name=input.next();
Student stu=new Student();//保存學(xué)生信息,set保存信息
stu.set(ID,name);
Students.put(ID,stu);//通過put,添加id學(xué)生映射,保存id?
System.out.println("添加學(xué)生:"+Students.get(ID).name);
i++;//i加1
}else{//否則
System.out.println("id已存在");
continue;//繼續(xù)循環(huán)
public void testKeySet(){ //打印出所有學(xué)生
Set<String>keySet=Students.keySet();//通過KeySet方法,返回Map中的所有"鍵"的Set集合
//取得Students容量
System.out.println("總共有"+Students.size()+"名學(xué)生");//size返回
for(String stuid:keySet){//學(xué)生id
? ? ? ? ? Student st=Students.get(stuid);
? ? ? ? ? if(st!=null)//st如果不為空
? ? ? ? ? System.out.println("學(xué)生:"+st.id+":"+st.name);
? ? ? ? ? }
?}
//刪除學(xué)生
public void romoveStu(){
System.out.println("請輸入要刪除學(xué)生的數(shù)量:");
int a=input.nextInt();
while(true){
for(int i=1;i<=a;i++){
if(a>Students.size()){
System.out.println("數(shù)量大于學(xué)生總量!?。?);
continue;
}else if(a==0){
break;
}else{
System.out.print("請輸入要刪除的第"+i+"位學(xué)生ID:");
Student st=(Student)Students.get(ID);
? ? ? ? ? ? ? ? ? if(st==null){?
System.out.println("id不存在");
Students.remove(ID);
System.out.println("成功刪除學(xué)生:"+st.name);
//通過entrySet方法來遍歷Map
public void testentrySet(){//輸出被刪除后剩余的學(xué)生
Set<Entry<String,Student>>entrySet=Students.entrySet();
for (Entry<String, Student> entry : entrySet) {
System.out.println("剩余學(xué)生id:"+entry.getKey());//對應(yīng)的id
System.out.println("學(xué)生姓名:"+entry.getValue().name);//對應(yīng)的值
//修改學(xué)生內(nèi)容
public void testModifly(){
System.out.println("請輸入要修改的學(xué)生id:");
String stuId = input.next();
Student st=Students.get(stuId);//保存當(dāng)前id
if(st==null){
System.out.println("當(dāng)前ID所對應(yīng)的學(xué)生:"+st.name);
System.out.println("請輸入新的學(xué)生姓名:");
String id;
Student newStu=new Student();//重新保存信息
newStu.set(stuId,name);
Students.put(stuId,newStu);//屬性.put(信息變量名,對象名)
System.out.println("修改成功!:");
System.out.println("修改后的學(xué)生:");
testKeySet();
//測試map中是否包含某個key值或value值
public void testContainsKeyOrVaule(){
System.out.print("請輸入要查詢的學(xué)生id:");
String id=input.next();
System.out.println("您輸入的學(xué)生:"+id+","+"是否存在:"+Students.containsKey(id));
if(Students.containsKey(id)){
? ? System.out.println("學(xué)生姓名:"+Students.get(id).name); //輸出和id對應(yīng)的姓名
//containsValue方法判斷學(xué)生是否存在
public void testContainsKeyOrVaule1(){
System.out.println("請輸入學(xué)生姓名:");
if(Students.containsValue(name)){
System.out.println("包含學(xué)生"+name+"對應(yīng)的id"+Students.get(name).id);
System.out.println("不包含");
public static void main(String[] args) {
Maptest p=new Maptest();
p.testPut();
p.testKeySet();
p.romoveStu();
p.testentrySet();
p.testKeySet();//刪除學(xué)生后在調(diào)用所有學(xué)生的方法是刪除學(xué)生后的學(xué)生
//p.testModifly();//修改學(xué)生
p.testContainsKeyOrVaule();
p.testContainsKeyOrVaule1();
public class Student {
String id;//學(xué)生
String name;
static Set <Class>cla;//學(xué)生所選的課程信息屬性
public ?Student(String id,String name){//將最終的值賦給構(gòu)造方法
this.name = name;
this.id=id;
this.cla=new HashSet<Class>();
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Student))
Student other = (Student) obj;
if (id == null) {
if (other.id != null)
} else if (!id.equals(other.id))
if (name == null) {
if (other.name != null)
} else if (!name.equals(other.name))
慕標(biāo)6149872
首先,你得把代碼貼出來
東方的小王 提問者
舉報(bào)
Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧
3 回答為什么我寫上了hashcode方法還是返回false?
1 回答為什么我寫了hashcode方法后還是返回false?
5 回答為什么我寫了hashcode方法后還是返回false?
2 回答為什么我重寫了equals方法結(jié)果還是返回false呢
2 回答求助。。。有沒有跟我一樣添加了hashcode和equals還是返回false的
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2017-08-27
錯誤如上圖。
2017-04-22
public class Maptest {
Scanner input = new Scanner(System.in);
//private static final String ID = null;
private Map<String,Student> Students;
public Maptest(){
this.Students=new HashMap<String,Student>();
}
public void testPut(){
Scanner input=new Scanner(System.in);
int i=1;
while(i<=3){//3個學(xué)生
System.out.print("請輸入第"+i+"學(xué)生ID:");
String ID=input.next();
Student st = Students.get(ID);//保存id,判斷id是否被占用
if(st==null){//如果當(dāng)前id為空
System.out.print("請輸入學(xué)生姓名:");
String name=input.next();
Student stu=new Student();//保存學(xué)生信息,set保存信息
stu.set(ID,name);
Students.put(ID,stu);//通過put,添加id學(xué)生映射,保存id?
System.out.println("添加學(xué)生:"+Students.get(ID).name);
i++;//i加1
}else{//否則
System.out.println("id已存在");
continue;//繼續(xù)循環(huán)
}
}
}
public void testKeySet(){ //打印出所有學(xué)生
Set<String>keySet=Students.keySet();//通過KeySet方法,返回Map中的所有"鍵"的Set集合
//取得Students容量
System.out.println("總共有"+Students.size()+"名學(xué)生");//size返回
for(String stuid:keySet){//學(xué)生id
? ? ? ? ? Student st=Students.get(stuid);
? ? ? ? ? if(st!=null)//st如果不為空
? ? ? ? ? System.out.println("學(xué)生:"+st.id+":"+st.name);
? ? ? ? ? }
?}
//刪除學(xué)生
public void romoveStu(){
Scanner input=new Scanner(System.in);
System.out.println("請輸入要刪除學(xué)生的數(shù)量:");
int a=input.nextInt();
while(true){
for(int i=1;i<=a;i++){
if(a>Students.size()){
System.out.println("數(shù)量大于學(xué)生總量!?。?);
continue;
}else if(a==0){
break;
}else{
}
System.out.print("請輸入要刪除的第"+i+"位學(xué)生ID:");
String ID=input.next();
Student st=(Student)Students.get(ID);
? ? ? ? ? ? ? ? ? if(st==null){?
System.out.println("id不存在");
continue;
}
Students.remove(ID);
System.out.println("成功刪除學(xué)生:"+st.name);
}
break;
}
}
//通過entrySet方法來遍歷Map
public void testentrySet(){//輸出被刪除后剩余的學(xué)生
Set<Entry<String,Student>>entrySet=Students.entrySet();
for (Entry<String, Student> entry : entrySet) {
System.out.println("剩余學(xué)生id:"+entry.getKey());//對應(yīng)的id
System.out.println("學(xué)生姓名:"+entry.getValue().name);//對應(yīng)的值
}
}
//修改學(xué)生內(nèi)容
public void testModifly(){
System.out.println("請輸入要修改的學(xué)生id:");
Scanner input = new Scanner(System.in);
while(true){
String stuId = input.next();
Student st=Students.get(stuId);//保存當(dāng)前id
if(st==null){
System.out.println("id不存在");
continue;
}else{
System.out.println("當(dāng)前ID所對應(yīng)的學(xué)生:"+st.name);
System.out.println("請輸入新的學(xué)生姓名:");
String name=input.next();
String id;
Student newStu=new Student();//重新保存信息
newStu.set(stuId,name);
Students.put(stuId,newStu);//屬性.put(信息變量名,對象名)
System.out.println("修改成功!:");
System.out.println("修改后的學(xué)生:");
testKeySet();
break;
}
}
}
//測試map中是否包含某個key值或value值
public void testContainsKeyOrVaule(){
System.out.print("請輸入要查詢的學(xué)生id:");
String id=input.next();
System.out.println("您輸入的學(xué)生:"+id+","+"是否存在:"+Students.containsKey(id));
if(Students.containsKey(id)){
? ? System.out.println("學(xué)生姓名:"+Students.get(id).name); //輸出和id對應(yīng)的姓名
}
}
//containsValue方法判斷學(xué)生是否存在
public void testContainsKeyOrVaule1(){
System.out.println("請輸入學(xué)生姓名:");
String name=input.next();
if(Students.containsValue(name)){
System.out.println("包含學(xué)生"+name+"對應(yīng)的id"+Students.get(name).id);
}else{
System.out.println("不包含");
}
}
public static void main(String[] args) {
Maptest p=new Maptest();
p.testPut();
p.testKeySet();
p.romoveStu();
p.testentrySet();
p.testKeySet();//刪除學(xué)生后在調(diào)用所有學(xué)生的方法是刪除學(xué)生后的學(xué)生
//p.testModifly();//修改學(xué)生
p.testContainsKeyOrVaule();
p.testContainsKeyOrVaule1();
}
}
2017-04-19
public class Student {
String id;//學(xué)生
String name;
static Set <Class>cla;//學(xué)生所選的課程信息屬性
public ?Student(String id,String name){//將最終的值賦給構(gòu)造方法
this.name = name;
this.id=id;
this.cla=new HashSet<Class>();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Student))
return false;
Student other = (Student) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
2017-04-18
首先,你得把代碼貼出來