HashMap集合的使用

来源:互联网 发布:37大闹天宫进阶数据 编辑:程序博客网 时间:2024/05/16 06:21

学生与就读的学校

第一部分:

public class student {

 

private Stringname;        //定义学生名字

private Stringschool;      //定义学校

public student(String name, String school) {

this.name = name;

this.school = school;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSchool() {

return school;

}

public void setSchool(String school) {

this.school = school;

}

 

@Override

public String toString() {

return "姓名:" +name + ",就读学校:" +school+"\n";

}

}


第二部分:

public class demo {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

student s1=new student("张三","清华大学");

student s2=new student("李四","北京大学");

student s3=new student("王五","香港大学");

Map m=new HashMap();

m.put("", s1);

m.put("", s2);

m.put("", s3);

System.out.println("键集:"+m.keySet());

System.out.println("值集:"+m.values());

System.out.println("-值对集合:"+m);

if (m.containsKey("")) {

System.out.println(m.get(""));

} else {

System.out.println("张三不见了!");

}

m.remove("");

System.out.println(m.get(""));

}

}