Java HashMap-获取与键关联的所有值
我创建了一个HashMap,其中每个键都包含一个ArrayList作为值。我很难理解如何获取与键关联的ArrayList并获取其中存储的所有值。请参阅下面运行程序时出现的错误Java HashMap-获取与键关联的所有值,java,hashmap,Java,Hashmap,我创建了一个HashMap,其中每个键都包含一个ArrayList作为值。我很难理解如何获取与键关联的ArrayList并获取其中存储的所有值。请参阅下面运行程序时出现的错误 import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Itera
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
public class Flights {
private HashMap<String, ArrayList<String>> flights = new HashMap<String, ArrayList<String>>();
private void readFlights(String filename) {
try {
BufferedReader bf = new BufferedReader(new FileReader(filename));
while (true) {
String line = bf.readLine();
if (line == null) {
break;
}
if (!line.isEmpty()) {
String fromCity = line.substring(0, line.indexOf("-"));
String toCity = line.substring(line.indexOf(">") + 2);
ArrayList<String> city = flights.get(fromCity);
if (city != null) {
city.add(toCity);
} else {
ArrayList<String> destinations = new ArrayList<String>();
destinations.add(toCity);
flights.put(fromCity, destinations);
}
}
}
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void printCities() {
Iterator<String> fi = flights.keySet().iterator();
while (fi.hasNext()) {
String next = fi.next();
System.out.println(next + "-> "+ flights.get(next));
}
}
@SuppressWarnings("resource")
private void printWelcome() {
System.out
.println("Welcome to the flight planner.\nHere is a list of all of our cities:");
printCities();
System.out.print("Enter the starting city.");
Scanner in = new Scanner(System.in);
String input = in.nextLine();
System.out.println("from " + input + " you can fly directly to:");
printAvailableFlights(input);
}
private void printAvailableFlights(String city) {
ArrayList<String> origin = flights.get(city);
for (String cities: origin) {
System.out.println(cities);
}
}
public static void main(String[] args) {
Flights f = new Flights();
f.readFlights("flights.txt");
f.printWelcome();
}
}
下面是我在运行程序时在控制台中看到的内容:
Welcome to the flight planner.
Here is a list of all of our cities:
Honolulu -> [New York, San Francisco]
Denver -> [San Jose]
Anchorage -> [New York, San Jose]
San Francisco -> [New York, Honolulu, Denver]
New York -> [Anchorage, San Jose, San Francisco, Honolulu]
San Jose -> [San Francisco, Anchorage]
Enter the starting city.Denver
from Denver you can fly directly to:
Exception in thread "main" java.lang.NullPointerException
at Flights.printAvailableFlights(Flights.java:64)
at Flights.printWelcome(Flights.java:59)
at Flights.main(Flights.java:72)
这条线
String fromCity = line.substring(0, line.indexOf("-"));
在起始城市后留下一个空格,因为这是文本文件的格式。您的钥匙看起来像“丹佛”
和“圣何塞”
。您应该将其更改为:
String fromCity = line.substring(0, line.indexOf("-") - 1);
此外,使用整个分隔符“->”
更有意义,因为有些城市中有破折号,例如
NullPointerException
被抛出,因为Map
如果密钥不存在,则返回null。您应该在PrintAvailableFlaghts
中说明这一点,您应该使用google Guava的Multimap-如果您不能回答您的问题,但我觉得我应该指出,您没有将一个键映射到多个值,您正在将一个键映射到列表
。您应该构建一个不包含从文件读取和处理异常的分散注意力/复杂性的映射。当我在printAvailableFlats方法中指定“origin”值时,它返回null
您认为它返回null是什么意思?您在下一行取消对它的引用,因此如果它flights.get(city)
返回null,您将得到一个NPE。你是说这个列表是空的吗?我想是因为你没有提供一个可运行的示例,也没有提供你正在阅读的文件的示例。你会注意到还没有发布答案,但是评论中有很多猜测。这是因为您没有提供足够的信息来准确诊断您的问题。谢谢您#Radiodef,您结束了两个小时的拔头发课程。好极了!
String fromCity = line.substring(0, line.indexOf("-") - 1);