使用Java

时间:2016-02-11 08:39:07

标签: java hashmap

我有这样的地图。

Map<String , String> studentGrades = new HashMap<String, String>();

    //Add Key/Value pairs
    studentGrades.put("Alvin", "A+");
    studentGrades.put("Alan", "A");
    studentGrades.put("Becca", "A-");
    studentGrades.put("Sheila", "B+");

我按照以下方式迭代地图。

for(String key: studentGrades.keySet()){
        System.out.println(key  +" :: "+ studentGrades.get(key));
    }

但是,在这张地图中,我想检查键“Alvin”是否与值“A +”一起出现。我无法理解如何做到这一点。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

你可以使用这个功能: -

boolean foo(String key, String value, Map<String , String> sG) {
    if (sG != null){
        if (sG.containsKey(key)){
            if ((sG.get(key)).equals(value)){
                return true;
            }
        }
    }
    return false;
}

答案 1 :(得分:0)

享受:oD (你最好不要将成绩保存为纯粹的琴弦:))

public enum Grades {
    APLUS("A+"),A("A"),AMINUS("A-"),
    BPLUS("B+"),B("B"),BMINUS("B-"),
    CPLUS("C+"),C("C"),CMINUS("C-"),
    DPLUS("D+"),D("D"),DMINUS("D-"),
    EPLUS("E+"),E("E"),EMINUS("E-"),
    FPLUS("F+"),F("F"),FMINUS("F-");

    String stringVal;

    private Grades(String stringVal) {
        this.stringVal = stringVal;
    }

    @Override
    public String toString() {
        return this.stringVal;
    }
}

public static void main(String[] args) {
        HashMap<String,Grades> studentGrades= new HashMap<>();
        studentGrades.put("Alvin", Grades.APLUS);
        studentGrades.put("Alan", Grades.A);
        studentGrades.put("Becca", Grades.AMINUS);
        studentGrades.put("Sheila", Grades.BPLUS);


        try {
            //A+
            System.out.println(getGradeByStudentName("Alvin", studentGrades));
        } catch (Exception e) {
            System.out.println("Student not found");
        }

        try {
            //true
            System.out.println(isStudentGrade("Becca", Grades.AMINUS,studentGrades));
        } catch (Exception e) {
            System.out.println("Student not found");
        }

    }


    /**
     * Obtain grade by the student name
     * @param studentName name of the student
     * @param source source data
     * @return {@link Grades} value 
     * @throws Exception throws exception while student not found or data source is null
     */
    public static Grades getGradeByStudentName(String studentName,HashMap<String, Grades> source) throws Exception{
        if(source!=null && source.containsKey(studentName)){
            return source.get(studentName);
        }
        throw new Exception("Student not found in database or null input data");
    }

    /**
     * Get boolean value if given student in param has grade in param
     * @param studentName name of the student
     * @param expectedGrade expected grade
     * @param source input source data 
     * @return true, if there is student with given name and has grade in parameter, false if there is a student but has another grade
     * @throws Exception if data source is null or student with given name is not found
     */
    public static boolean isStudentGrade(String studentName, Grades expectedGrade, HashMap<String, Grades> source) throws Exception{
            Grades grade = getGradeByStudentName(studentName, source);
            return grade.equals(expectedGrade);
    }
  

FYI   对于hashmap中的Keys和值的迭代,您可以使用以下

HashMap<String, String> mapName = new HashMap<>();

        for (String actualKey : mapName.keySet()) {
            //eg. String value = mapName.get(actualKey);
        }

        for (String actualVal : mapName.values()) {

        }