HashMap <string,student =“”>在Student class </string,>中搜索实例

时间:2013-12-04 11:08:55

标签: search hashmap

我正在使用两个辅助类(Student,Helper)以及一个主类。

在Student类中,我有以下构造函数:

Student(String iName, String iMajor, int iNumber) {
    name = iName;
    major = iMajor;
    number = iNumber;
}

在Helper类中,我按如下方式声明HashMap:

HashMap<String, Student> students = new HashMap<String, Student>();

现在,我已经编写了一些方法,用于将新学生添加(放入)HashMap构造,以及根据名称检索学生信息的方法。

//Adding new students
Student s1 = new Student("Alex", "Biology", 19);
Student s2 = new Student("Brian", "Chemistry", 20);
Student s3 = new Student("Tom", "Biology", 20);
//etc...

//Get student from name (key)
public Student getFromKey(String key) {
    return students.get(key);
}

我现在正在寻找一种能够根据专业或数字查找所有学生的方法。例如,电话:

helper.getStudents("Biology");

应该归还所有学习生物学的学生。我想这个方法看起来像:

public Student getStudents(String searchItem) {
    for(Students st : students.values()) {
        if(searchItem.equals(??)) {
            return st;
//Something like this. 

但是,我似乎无法弄清楚如何访问这些值。所有类都有适当的getter和setter方法,程序运行正常。任何帮助都非常感谢!

1 个答案:

答案 0 :(得分:1)

假设存在Map

HashMap<String, Student> students = new HashMap<String, Student>();

以下方法可行:

   public Student getStudents(String searchItem) {
    for(Map.Entry<String,Student> entry : students.entrySet()) {
        Student student = entry.getValue();
        //perform conditional logic here
    }

如果您需要,这是一个更完整的示例:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Student {

private String name;
private String major;
private int number;

public Student(String name, String major, int number) {
    super();
    this.name = name;
    this.major = major;
    this.number = number;
}

public static void main(String[] args) {
    List<String> names = Arrays.asList("Joe", "Jack", "John","James");
    List<String> majors = Arrays.asList("English","Math","Geography");
    Map<String,Student> students = new HashMap<String,Student>();
    for(int i = 0; i < 100; i++){
        Collections.shuffle(names);
        Collections.shuffle(majors);
        students.put(names.get(0) + String.valueOf(i), new Student(names.get(0), majors.get(0), i));
}

    List<Student> mathMajors = getStudents(students, "Math");
    for(Student student:mathMajors){
        System.out.println(student.name);
        System.out.println(student.major);
    }
}

public static List<Student> getStudents(Map<String,Student> students, String searchToken){
    List<Student> results = new ArrayList<Student>();
    for(Entry<String,Student> entry:students.entrySet()){
        if(entry.getValue().getMajor().equalsIgnoreCase(searchToken)){
        results.add(entry.getValue());
    }
}
return results;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getMajor() {
return this.major;
}

public void setMajor(String major) {
this.major = major;
}

public int getNumber() {
return this.number;
}

public void setNumber(int number) {
this.number = number;
}

}

On Github