首页 > 解决方案 > 如何判断一个键是否包含在 Hashmap 内的非命名 Hashmap 中?Java、JavaFX

问题描述

我目前正在为学校做作业,我应该在这样的哈希图中创建一个哈希图:

Map<String, Map<String, Integer>> girlsByYear = new HashMap<>();
Map<String, Map<String, Integer>> boysByYear = new HashMap<>();

根据教授的说法,完成作业不需要任何其他地图。这些地图通过访问包含名称、性别和按年份排列的婴儿姓名的包中的文件来向它们添加元素。程序编译完成后,会创建一个 JavaFX 图表,要求输入女孩或男孩的名字。输入后,图表会按年份显示该名称的流行程度。

目前,我已经弄清楚了大部分,但我无法理解如何在没有第一个哈希图的键的情况下访问第一个哈希图中的哈希图。我的意思是,我应该通过文本框对 JavaFX 类执行检查,以检查名称是否在 Hashmap 中。这是我的代码:

public class NameHelper {

// Declare the hash maps.
Map<String, Map<String, Integer>> girlsByYear = new HashMap<>();

Map<String, Map<String, Integer>> boysByYear = new HashMap<>();

// Declare addition variables.
String firstWord = "";
String secondWord = "";
String thirdWord = "";
Integer rank;

String fileName;

// This method will load the files from the data package, review the files,
// and add each item respectively to either map.
public void load() throws FileNotFoundException {
    File dir = new File("src/data");
    File [] files = dir.listFiles();

    // for each file in the directory...
    for (File f : files)
    {

        // Get the file name and split the year from it to add to each name.
        String newFileName = f.getName();
        fileName = newFileName.replaceAll("[yobtxt.]","");

        Scanner scanner = new Scanner(f);

        // While the files are not empty.
        while(scanner.hasNextLine()) {

            // If the second column split by a delimiter is M then add the information
            // to the boys.  Else girls.

            String input = scanner.nextLine();  
            // Set the input to string values to enter into each hash map.
            String initial = input.split(",")[1];
            firstWord = fileName;
            secondWord = (input.split(",")[0]).toLowerCase();
            thirdWord = input.split(",")[2];
            rank = Integer.parseInt(thirdWord);

            // Use a switch statements since if statements aren't working.
            switch(initial) {
            case "M":
                boysByYear.put(firstWord, new HashMap<String, Integer>());
                boysByYear.get(firstWord).put(secondWord, rank);


                break;
            case "F":
                girlsByYear.put(firstWord, new HashMap<String, Integer>());
                girlsByYear.get(firstWord).put(secondWord, rank);

                break;
                default:
                    System.out.println("This is an issue");
                    break;
            }

        }

        // Close the scanner.
        scanner.close();

    }

}


// This method will return a sorted set of years by getting the keyset from the hashmaps.
public Set<String> getYears() {

    // Create the set.
    Set<String> set = new HashSet<>();

    // Add all the years of the listed by file name.
    for(String key : girlsByYear.keySet()) {
        set.add(key);
    }

    // Convert the set to a sorted set.
    TreeSet<String> treeSet = new TreeSet<>(set);
    return treeSet;
}

// This method will return true if the supplied name is found in the data structure.
// Use the gender input to determine which map to search by using "containsKey".
public boolean isNamePresent(String name, String gender) {


    if(gender == "M") {


        //Check if the name is within the map's map.
        if(boysByYear.get(name).containsKey(name)) {
            return true;
        }

    }
    else if(gender == "F") {
        if(girlsByYear.containsKey(name.toLowerCase())) {
            return true;
        }
    }
    return false;
}

我需要帮助的部分是 isNamePresent 方法。我需要检查名称是否在以这种格式设置的第二个哈希映射的键中(字符串年份,哈希映射(字符串名称,整数排名))

任何帮助或指导将不胜感激!

附加说明:图表的 JavaFx 部分由教授提供。

标签: javajavafxhashmapnested

解决方案


您首先需要解决的一件事是使用==. gender除非作为参数传递的字符串都是字符串文字,否则这不起作用。您需要改为使用equals,请参阅如何在 Java 中比较字符串?switch自动执行此操作)。

此外,您应该通过将映射检索到局部变量来避免重复代码:

Map<String, Map<String, Integer>> map;
switch (gender) {
    case "M":
        map = boysByYear;
        break;
    case "F":
        map = girlsByYear;
        break;
    default:
        return false; // alternatively throw new IllegalArgumentException();
}

要找出是否至少有一个映射包含name作为键,请遍历所有值并检查映射:

final String nameLower = name.toLowerCase();
return map.values().stream().anyMatch(m -> m.containsKey(nameLower));

顺便说一句:您需要修复读取数据的方式。否则,由于您替换了Map. 此外,我建议存储结果split而不是调用 3 次。也不要将字段用作仅在循环中需要的变量,并选择更具描述性的变量名称:

Map<String, Integer> boys = new HashMap<>();
Map<String, Integer> girls = new HashMap<>();

boysByYear.put(fileName, boys);
girlsByYear.put(fileName, girls);

while(scanner.hasNextLine()) {

    // If the second column split by a delimiter is M then add the information
    // to the boys.  Else girls.

    String input = scanner.nextLine();
    String[] parts = input.split(",");

    // Set the input to string values to enter into each hash map.
    String gender = parts[1];

    String name = parts[0].toLowerCase();
    int rank = Integer.parseInt(parts[2]);

    switch(gender) {
        case "M":
            boys.put(name, rank);
            break;
        case "F":
            girls.put(name, rank);
            break;
        default:
            System.out.println("This is an issue");
            break;
    }

}

推荐阅读