0

I've a map like this .

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+");

I'm iterating the map as follows.

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

But, in this map, I want to check whether key "Alvin" is present with the value "A+". I couldn't understand how to do it. Any ideas?

9
  • 1
    Start by understanding that you can use .get("") to get an entry from the map, and you can use equals() to compare objects Commented Feb 11, 2016 at 8:41
  • I know equals() is for compare.. I want to see a matching key & value pair exist or not Commented Feb 11, 2016 at 8:42
  • 2
    if ("A+".equals(studentGrades.get("Alvin"))) { ... } Commented Feb 11, 2016 at 8:43
  • Note: don't iterate the map like that if you want both key and associated value: use for (Map.Entry<String, String> entry: studentGrades.entrySet()) {, and then use entry.getKey() and entry.getValue() to get the key and value respectively. Commented Feb 11, 2016 at 8:43
  • 1
    @Andreas, ("A+".equals(studentGrades.get("Alvin")) this is for comparing objects..What I want is to see if "Alvin" is assosiated with "A+" or not. I hope you understood my point. I'm not going to check for string comparision. Commented Feb 11, 2016 at 8:46

2 Answers 2

4

you can use this function : -

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Enjoy that :oD (You will have better to don't save grades as pure strings :) )

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 For iterate over Keys and values in hashmap you can use following

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

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

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

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.