根据学员英文名找到学员对象

学生类

 

package com.twod1z;
/**
* @program: com.twod1z
* @description:根据学员英文名找到学员对象(学生类)
* @author: Mr.Lin
* @create: 2019年7月27日
**/
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.twod1z;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @program: com.twod1z
* @description:根据学员英文名找到学员对象(测试类)
* @author: Mr.Lin
* @create: 2019年7月27日
**/
public class Text {
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("Jack", s1);
m.put("Mary", s2);
m.put("Michael", s3);

Set<String>s = m.keySet();

for (String str : s) {
Student stu = m.get(str);
System.out.println(str+"对应的学员姓名是:"+stu.getName()+";性别是:"+stu.getSex());
}
}

}

   

原文地址:https://www.cnblogs.com/lpbk/p/11257064.html