各位大神,為什么我的map的contains方法弄不了?。?/h1>
package listarrray;
public class Student {
? ? String id;
? ? String name;
? ? Courses selectcourses;
? ? public Student(String id,String name){
? ? ? ? this.id=id;
? ? ? ? this.name=name;
? ? }
? ? public Student(){
? ? }
? ? ? ? @Override
? ? public boolean equals(Object obj){
? ? ? ? if(this==obj){
? ? ? ? ? ? return true;
? ? ? ? }else{
? ? ? ? ? ? if(obj==null){
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? if(!(obj instanceof Student)){
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? Student std=(Student)obj;
? ? ? ? ? ? ? ? ? ? if(this.name==null){
? ? ? ? ? ? ? ? ? ? ? ? if(std.name==null){
? ? ? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? if(this.name.equals(std.name)){
? ? ? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ? @Override
? ? public int hashCode() {
? ? ? ? final int t=31;
? ? ? ? int hash = 1;
? ? ? ? hash = t*hash+((name==null)?0:name.hashCode());
? ? ? ? return hash;
? ? }
? ??
}
package listarrray;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class MapTest {
? ? Map<String,Student> students;
? ? public MapTest(){
? ? ? ? students=new HashMap<String,Student>();
? ? ? ? int i=0;
? ? ? ? while(i<3){
? ? ? ? ? ? System.out.print("請(qǐng)輸入學(xué)生的ID:");
? ? ? ? ? ? Scanner in=new Scanner(System.in);
? ? ? ? ? ? String ID=in.next();
? ? ? ? ? ? Student st=students.get(ID);
? ? ? ? ? ? if(st==null){
? ? ? ? ? ? ? ? System.out.print("請(qǐng)輸入學(xué)生姓名:");
? ? ? ? ? ? ? ? String newstudent=in.next();
? ? ? ? ? ? ? ? Student NewStudent=new Student(ID,newstudent);
? ? ? ? ? ? ? ? NewStudent.name=newstudent;
? ? ? ? ? ? ? ? students.put(ID, NewStudent);
? ? ? ? ? ? ? ? System.out.println("添加學(xué)生"+NewStudent.name+"成功!");
? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? System.out.println("該ID已被占用!");
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ? public void containstest(){
? ? ? ? System.out.print("請(qǐng)輸入想要查找的學(xué)生的key:");
? ? ? ? Scanner in=new Scanner(System.in);
? ? ? ? String keyin=in.next();
? ? ? ? if(students.containsKey(keyin)){
? ? ? ? ? ? System.out.println("Yes,Someone here!his name is: "+students.get(keyin).name);
? ? ? ? }else{
? ? ? ? ? ? System.out.println("Sorry,No one here!");
? ? ? ? }
? ? ? ? System.out.print("請(qǐng)輸入想要查找的學(xué)生的name:");
? ? ? ? String namein=in.next();
? ? ? ? if(students.containsKey(new Student(null,namein))){
? ? ? ? ? ? System.out.println("Yes,Someone here!his name is: "+namein);
? ? ? ? }else{
? ? ? ? ? ? System.out.println("Sorry,No one here!");
? ? ? ? }
? ? }
? ??
? ? public void getMap(){
// ? ? ? ?keyset方法是將map中的Key集合轉(zhuǎn)化為set集合;
? ? ? ? Set<String> studentid=students.keySet();
? ? ? ? System.out.println("一共有"+students.size()+"個(gè)學(xué)生");
? ? ? ? for (String sss:studentid) {
? ? ? ? ? ? Student st1=students.get(sss);
? ? ? ? ? ? if(st1!=null){
? ? ? ? ? ? ?System.out.println("學(xué)生:"+students.get(sss).name);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ? public void removemap(){
? ? ? ? Scanner in1=new Scanner(System.in);
? ? ? ? while(true){
? ? ? ? ? ? System.out.print("請(qǐng)輸入要?jiǎng)h除的學(xué)生的ID:");
? ? ? ? ? ? String removeID=in1.next();
? ? ? ? ? ? Student removest=students.get(removeID);
? ? ? ? ? ? if(removest!=null){
? ? ? ? ? ? ? ? students.remove(removeID);
? ? ? ? ? ? ? ? System.out.println("成功刪除學(xué)生"+removest.name);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? System.out.println("該學(xué)生不存在!");
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ? public void entrytest(){
? ? ? ? //系統(tǒng)的entry有屬于自己的泛型,需要把它改成對(duì)應(yīng)map集合的泛型;entryset方法是將map轉(zhuǎn)化為set集合的方法;
? ? ? ? Set<Entry<String, Student>>entryset=students.entrySet();
? ? ? ? System.out.println("所有學(xué)生:");
? ? ? ? for(Entry<String,Student> entry:entryset){
? ? ? ? //entry的set集合元素可調(diào)用getKey()和getValue()方法分別取得map元素的Key和Value;
? ? ? ? ? ? System.out.println("第"+entry.getKey()+"學(xué)生:"+entry.getValue().name);
? ? ? ? }
? ? }
? ??
? ? public void changemap(){
? ? ? ? Scanner in2=new Scanner(System.in);
? ? ? ? while(true){
? ? ? ? ? ? System.out.print("請(qǐng)輸入要修改的學(xué)生的ID:");
? ? ? ? ? ? String changeid=in2.next();
? ? ? ? ? ? Student changest=students.get(changeid);
? ? ? ? ? ? if(changest==null){
? ? ? ? ? ? ? ? System.out.println("輸入的ID不存在,請(qǐng)重新輸入!");
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println("該學(xué)生目前的名字為:"+changest.name);
? ? ? ? ? ? System.out.print("請(qǐng)輸入修改的名字:");
? ? ? ? ? ? String changename=in2.next();
? ? ? ? ? ? Student newstudent1=new Student(changeid,changename);
? ? ? ? ? ? students.put(changeid, newstudent1);
? ? ? ? ? ? System.out.println("修改成功!");
? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ??
? ? public static void main(String[] args) {
? ? ? ? MapTest ttt=new MapTest();
? ? ? ? ttt.getMap();
? ? ? ? ttt.containstest();
// ? ? ? ?ttt.removemap();
// ? ? ? ?ttt.entrytest();
// ? ? ? ?ttt.changemap();
// ? ? ? ?ttt.entrytest();
? ? }
}
package listarrray;
public class Student {
? ? String id;
? ? String name;
? ? Courses selectcourses;
? ? public Student(String id,String name){
? ? ? ? this.id=id;
? ? ? ? this.name=name;
? ? }
? ? public Student(){
? ? }
? ? ? ? @Override
? ? public boolean equals(Object obj){
? ? ? ? if(this==obj){
? ? ? ? ? ? return true;
? ? ? ? }else{
? ? ? ? ? ? if(obj==null){
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? if(!(obj instanceof Student)){
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? Student std=(Student)obj;
? ? ? ? ? ? ? ? ? ? if(this.name==null){
? ? ? ? ? ? ? ? ? ? ? ? if(std.name==null){
? ? ? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? if(this.name.equals(std.name)){
? ? ? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ? @Override
? ? public int hashCode() {
? ? ? ? final int t=31;
? ? ? ? int hash = 1;
? ? ? ? hash = t*hash+((name==null)?0:name.hashCode());
? ? ? ? return hash;
? ? }
? ??
}
package listarrray;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class MapTest {
? ? Map<String,Student> students;
? ? public MapTest(){
? ? ? ? students=new HashMap<String,Student>();
? ? ? ? int i=0;
? ? ? ? while(i<3){
? ? ? ? ? ? System.out.print("請(qǐng)輸入學(xué)生的ID:");
? ? ? ? ? ? Scanner in=new Scanner(System.in);
? ? ? ? ? ? String ID=in.next();
? ? ? ? ? ? Student st=students.get(ID);
? ? ? ? ? ? if(st==null){
? ? ? ? ? ? ? ? System.out.print("請(qǐng)輸入學(xué)生姓名:");
? ? ? ? ? ? ? ? String newstudent=in.next();
? ? ? ? ? ? ? ? Student NewStudent=new Student(ID,newstudent);
? ? ? ? ? ? ? ? NewStudent.name=newstudent;
? ? ? ? ? ? ? ? students.put(ID, NewStudent);
? ? ? ? ? ? ? ? System.out.println("添加學(xué)生"+NewStudent.name+"成功!");
? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? System.out.println("該ID已被占用!");
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ? public void containstest(){
? ? ? ? System.out.print("請(qǐng)輸入想要查找的學(xué)生的key:");
? ? ? ? Scanner in=new Scanner(System.in);
? ? ? ? String keyin=in.next();
? ? ? ? if(students.containsKey(keyin)){
? ? ? ? ? ? System.out.println("Yes,Someone here!his name is: "+students.get(keyin).name);
? ? ? ? }else{
? ? ? ? ? ? System.out.println("Sorry,No one here!");
? ? ? ? }
? ? ? ? System.out.print("請(qǐng)輸入想要查找的學(xué)生的name:");
? ? ? ? String namein=in.next();
? ? ? ? if(students.containsKey(new Student(null,namein))){
? ? ? ? ? ? System.out.println("Yes,Someone here!his name is: "+namein);
? ? ? ? }else{
? ? ? ? ? ? System.out.println("Sorry,No one here!");
? ? ? ? }
? ? }
? ??
? ? public void getMap(){
// ? ? ? ?keyset方法是將map中的Key集合轉(zhuǎn)化為set集合;
? ? ? ? Set<String> studentid=students.keySet();
? ? ? ? System.out.println("一共有"+students.size()+"個(gè)學(xué)生");
? ? ? ? for (String sss:studentid) {
? ? ? ? ? ? Student st1=students.get(sss);
? ? ? ? ? ? if(st1!=null){
? ? ? ? ? ? ?System.out.println("學(xué)生:"+students.get(sss).name);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ? public void removemap(){
? ? ? ? Scanner in1=new Scanner(System.in);
? ? ? ? while(true){
? ? ? ? ? ? System.out.print("請(qǐng)輸入要?jiǎng)h除的學(xué)生的ID:");
? ? ? ? ? ? String removeID=in1.next();
? ? ? ? ? ? Student removest=students.get(removeID);
? ? ? ? ? ? if(removest!=null){
? ? ? ? ? ? ? ? students.remove(removeID);
? ? ? ? ? ? ? ? System.out.println("成功刪除學(xué)生"+removest.name);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? System.out.println("該學(xué)生不存在!");
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ? public void entrytest(){
? ? ? ? //系統(tǒng)的entry有屬于自己的泛型,需要把它改成對(duì)應(yīng)map集合的泛型;entryset方法是將map轉(zhuǎn)化為set集合的方法;
? ? ? ? Set<Entry<String, Student>>entryset=students.entrySet();
? ? ? ? System.out.println("所有學(xué)生:");
? ? ? ? for(Entry<String,Student> entry:entryset){
? ? ? ? //entry的set集合元素可調(diào)用getKey()和getValue()方法分別取得map元素的Key和Value;
? ? ? ? ? ? System.out.println("第"+entry.getKey()+"學(xué)生:"+entry.getValue().name);
? ? ? ? }
? ? }
? ??
? ? public void changemap(){
? ? ? ? Scanner in2=new Scanner(System.in);
? ? ? ? while(true){
? ? ? ? ? ? System.out.print("請(qǐng)輸入要修改的學(xué)生的ID:");
? ? ? ? ? ? String changeid=in2.next();
? ? ? ? ? ? Student changest=students.get(changeid);
? ? ? ? ? ? if(changest==null){
? ? ? ? ? ? ? ? System.out.println("輸入的ID不存在,請(qǐng)重新輸入!");
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println("該學(xué)生目前的名字為:"+changest.name);
? ? ? ? ? ? System.out.print("請(qǐng)輸入修改的名字:");
? ? ? ? ? ? String changename=in2.next();
? ? ? ? ? ? Student newstudent1=new Student(changeid,changename);
? ? ? ? ? ? students.put(changeid, newstudent1);
? ? ? ? ? ? System.out.println("修改成功!");
? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ??
? ? public static void main(String[] args) {
? ? ? ? MapTest ttt=new MapTest();
? ? ? ? ttt.getMap();
? ? ? ? ttt.containstest();
// ? ? ? ?ttt.removemap();
// ? ? ? ?ttt.entrytest();
// ? ? ? ?ttt.changemap();
// ? ? ? ?ttt.entrytest();
? ? }
}
2017-05-07
查找學(xué)生姓名那里的?if(students.containsKey(new Student(null,namein)))
改成?if(students.containsValue(new Student(null,namein)))
就好啦!
望采納