根据学员英文名找到学员对象
学生类代码:
package com.collections; public class Student { private String name; private char sex; public Student(){} public Student(String name,char sex){ this.name = name; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } }
测试类代码:
package com.collections; import java.util.HashMap; import java.util.Map; import java.util.Set; public class Test { public static void main(String[] args) { Student s1 = new Student("张三",'男'); Student s2 = new Student("李四",'女'); Student s3 = new Student("王五",'男'); Map<String, Student> m = new HashMap<String, Student>(); m.put("zhangsan", s1); m.put("lisi",s2); m.put("wangwu",s3); Set<String> s = m.keySet(); for(String str : s){ Student stu = m.get(str); System.out.println(str+"对应学员的姓名是:"+stu.getName()+";性别是:"+stu.getSex()); } } }
运行结果: