Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java在Hashmap中获取匹配的键和值_Java_Hashmap - Fatal编程技术网

使用Java在Hashmap中获取匹配的键和值

使用Java在Hashmap中获取匹配的键和值,java,hashmap,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

我有一张这样的地图

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

但是,在这个映射中,我想检查键“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;
}
boolean foo(字符串键、字符串值、映射sG){
如果(sG!=null){
如果(集装箱专用标志(图例)){
如果((sG.get(key))等于(value)){
返回true;
}
}
}
返回false;
}

您可以使用此功能:-

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;
}
boolean foo(字符串键、字符串值、映射sG){
如果(sG!=null){
如果(集装箱专用标志(图例)){
如果((sG.get(key))等于(value)){
返回true;
}
}
}
返回false;
}
享受这个:oD (最好不要将分数保存为纯字符串:))

公共枚举等级{
APLUS(“A+”),A(“A”),AMINUS(“A-”,
BPLUS(“B+”),B(“B”),BMINUS(“B-”,
CPLUS(“C+”)、C(“C”)、CMINUS(“C-”,
DPLU(“D+”),D(“D”),DMINUS(“D-”,
埃普勒斯(“E+”),埃米努斯(“E-”),
FPLU(“F+”),F(“F”),Fmin(“F-”;
字符串stringVal;
私人职系(String-stringVal){
this.stringVal=stringVal;
}
@凌驾
公共字符串toString(){
返回此.stringVal;
}
}
公共静态void main(字符串[]args){
HashMap studentGrades=新建HashMap();
学生成绩.put(“Alvin”,Grades.APLUS);
学生成绩.put(“艾伦”,成绩.A);
学生成绩。put(“Becca”,成绩。AMINUS);
学生成绩.put(“Sheila”,Grades.BPLUS);
试一试{
//A+
System.out.println(getGradeByStudentName(“Alvin”,studentGrades));
}捕获(例外e){
System.out.println(“未找到学生”);
}
试一试{
//真的
System.out.println(isStudentGrade(“Becca”,Grades.AMINUS,studentGrades));
}捕获(例外e){
System.out.println(“未找到学生”);
}
}
/**
*根据学生姓名获得分数
*@param studentName该学生的姓名
*@param源数据
*@return{@link Grades}值
*@throws Exception在未找到学生或数据源为空时引发异常
*/
公共静态成绩getGradeByStudentName(字符串studentName,HashMap源)引发异常{
if(source!=null&&source.containsKey(studentName)){
返回source.get(studentName);
}
抛出新异常(“数据库中未找到学生或输入数据为空”);
}
/**
*如果param中的给定学生在param中有分数,则获取布尔值
*@param studentName该学生的姓名
*@param expectedGrade预期等级
*@param源输入源数据
*@return true,如果参数中有名且有成绩的学生,则返回true;如果有学生但有其他成绩,则返回false
*若数据源为空或未找到具有给定名称的学生,@将引发异常
*/
公共静态布尔值isStudentGrade(字符串studentName、成绩expectedGrade、HashMap源)引发异常{
成绩等级=getGradeByStudentName(studentName,来源);
返回等级。等于(预期等级);
}
供参考 对于hashmap中的键和值的迭代,可以使用以下命令

HashMap-mapName=newhashmap();
对于(字符串actualKey:mapName.keySet()){
//例如,String value=mapName.get(actualKey);
}
对于(字符串实际值:mapName.values()){
}
享受这个:oD (最好不要将分数保存为纯字符串:))

公共枚举等级{
APLUS(“A+”),A(“A”),AMINUS(“A-”,
BPLUS(“B+”),B(“B”),BMINUS(“B-”,
CPLUS(“C+”)、C(“C”)、CMINUS(“C-”,
DPLU(“D+”),D(“D”),DMINUS(“D-”,
埃普勒斯(“E+”),埃米努斯(“E-”),
FPLU(“F+”),F(“F”),Fmin(“F-”;
字符串stringVal;
私人职系(String-stringVal){
this.stringVal=stringVal;
}
@凌驾
公共字符串toString(){
返回此.stringVal;
}
}
公共静态void main(字符串[]args){
HashMap studentGrades=新建HashMap();
学生成绩.put(“Alvin”,Grades.APLUS);
学生成绩.put(“艾伦”,成绩.A);
学生成绩。put(“Becca”,成绩。AMINUS);
学生成绩.put(“Sheila”,Grades.BPLUS);
试一试{
//A+
System.out.println(getGradeByStudentName(“Alvin”,studentGrades));
}捕获(例外e){
System.out.println(“未找到学生”);
}
试一试{
//真的
System.out.println(isStudentGrade(“Becca”,Grades.AMINUS,studentGrades));
}捕获(例外e){
System.out.println(“未找到学生”);
}
}
/**
*根据学生姓名获得分数
*@param studentName该学生的姓名
*@param源数据
*@return{@link Grades}值
*@throws Exception在未找到学生或数据源为空时引发异常
*/
公共静态成绩getGradeByStudentName(字符串studentName,HashMap源)引发异常{
if(source!=null&&source.containsKey(studentName)){
返回source.get(studentName);
}
抛出新异常(“数据库中未找到学生或输入数据为空”);
}
/**
*如果param中的给定学生在param中有分数,则获取布尔值
*@param studentName该学生的姓名
*@param expectedGrade预期等级
*@param源输入源数据
*@return true,如果参数中有名且有成绩的学生,则返回true;如果有学生但有其他成绩,则返回false
*若数据源为空或未找到具有给定名称的学生,@将引发异常
*/
公共静态布尔值isStudentGrade(字符串studentName、成绩expectedGrade、HashMap源)引发异常{
成绩等级=getGradeByStudentName(studentName,来源);
重新
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()) {

        }