簡體   English   中英

使用Java在Hashmap中獲取匹配的鍵和值

[英]Get matching key and value in Hashmap using Java

我有這樣的地圖。

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 +”。 我不知道該怎么做。 有任何想法嗎?

您可以使用此功能:-

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;
}

享受: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);
    }

僅供參考,要遍歷哈希圖中的鍵和值,可以使用以下命令

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

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

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

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM