В настоящее время я работаю над заданием для школы, где я должен создать хеш-карту в хеш-карте, например:
Map<String, Map<String, Integer>> girlsByYear = new HashMap<>();
Map<String, Map<String, Integer>> boysByYear = new HashMap<>();
По словам профессора, никаких других карт не требуетсявыполнить задание.Карты добавляют к ним элементы, получая доступ к файлам в пакете, которые содержат имена, пол и ранг имен детей по годам.Когда программа скомпилирована и завершена, создается диаграмма JavaFX, которая запрашивает имя девочки или мальчика.При вводе на диаграмме по годам отображается рейтинг популярности имени.
В настоящее время я понял большую часть этого, но я не могу понять, как получить доступ к Hashmap в первом Hashmap без ключа для первого hashmap.Под этим я подразумеваю, что я должен иметь проверку, выполняемую текстовым полем для класса 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, который настроен в этом формате (String year, HashMap (String name, Integer rank))
Любая помощь или руководство будет с благодарностью!
Дополнительные примечания: Раздел JavaFx для диаграммы предоставлен профессором.