我如何知道密钥是否包含在哈希图中的未命名哈希图中? Java,JavaFX

时间:2018-11-26 02:34:47

标签: java javafx hashmap nested

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

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方法。我需要检查名称是否在以这种格式设置的第二个哈希图的键中(字符串年份,HashMap(字符串名称,整数等级))

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

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

2 个答案:

答案 0 :(得分:1)

您需要首先解决的一件事是使用==比较字符串。除非两个作为gender参数传递的字符串都是字符串文字,否则此方法将无效。您需要改用equals,请参阅How do I compare strings in 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;
    }

}

答案 1 :(得分:0)

要访问内部哈希图而不知道外部图的键,您可以遍历外部图的每个条目。

for(Map.Entry<String, Integer> mapEntry: boysByYear.entrySet()){
// Get the innerMap and check if the name exists
    Map<String, Integer> innerMap = mapEntry.getValue();
        if(innerMap.containsKey(name)){
            return true;
        }
}