【问题标题】:Get matching key and value in Hashmap using Java使用Java在Hashmap中获取匹配的键和值
【发布时间】:2016-02-11 08:39:06
【问题描述】:

我有一张这样的地图。

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+”。我不明白该怎么做。有什么想法吗?

【问题讨论】:

  • 首先了解可以使用.get("")从地图中获取条目,并且可以使用equals()比较对象
  • 我知道 equals() 是用来比较的。我想看看匹配的键值对是否存在
  • if ("A+".equals(studentGrades.get("Alvin"))) { ... }
  • 注意:如果你想要 key 和关联的 value,不要这样迭代 map:使用 for (Map.Entry&lt;String, String&gt; entry: studentGrades.entrySet()) {,然后使用 entry.getKey()entry.getValue() 分别获取 key 和 value。
  • @Andreas, ("A+".equals(studentGrades.get("Alvin")) 这是用于比较对象..我想看看“Alvin”是否与“A+”相关联与否。我希望你明白我的意思。我不会检查字符串比较。

标签: java hashmap


【解决方案1】:

你可以使用这个功能:-

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

【讨论】:

    【解决方案2】:

    尽情享受吧 :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 中的键和值,您可以使用以下方法

    HashMap<String, String> mapName = new HashMap<>();
    
            for (String actualKey : mapName.keySet()) {
                //eg. String value = mapName.get(actualKey);
            }
    
            for (String actualVal : mapName.values()) {
    
            }
    

    【讨论】:

      猜你喜欢
      • 2016-02-13
      • 2021-05-31
      • 2020-04-06
      • 2018-06-10
      • 2010-11-25
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多