2014-09-22 77 views
0

我创建了一个HashMap,其中每个键包含一个ArrayList作为值。我无法理解如何获取与某个键关联的ArrayList并获取存储在其中的所有值。看到下面我运行程序时得到的错误。HashMap - 获取与键值相关的所有值

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

} 

这里是flights.txt文件:

San Jose -> San Francisco 
San Jose -> Anchorage 

New York -> Anchorage 
New York -> San Jose 
New York -> San Francisco 
New York -> Honolulu 

Anchorage -> New York 
Anchorage -> San Jose 

Honolulu -> New York 
Honolulu -> San Francisco 

Denver -> San Jose 

San Francisco -> New York 
San Francisco -> Honolulu 
San Francisco -> Denver 

,这里是我在控制台中看到运行程序时:

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) 
+1

您应该使用谷歌Guava的Multimap - 如果您可以http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.html – imrichardcole 2014-09-22 22:04:09

+0

为什么? HashMap有什么问题? – Brandon 2014-09-22 22:04:43

+2

与你的问题没有关系,但我觉得我应该指出,你不是将一个键映射到多个值,而是将一个键映射到List。 – azurefrog 2014-09-22 22:04:56

回答

1

此行

String fromCity = line.substring(0, line.indexOf("-")); 

启动后留出空间c因为这是你的文本文件的格式。您的密钥看起来像"Denver ""San Jose "。你应该把它改成这样:

String fromCity = line.substring(0, line.indexOf("-") - 1); 

此外,它将使使用整个分隔符" -> "更有意义,因为一些城市对他们有一个破折号,如Wilkes-Barre

NullPointerException因为Map如果密钥不存在则返回null。你应该在printAvailableFlights中说明这一点。

+0

谢谢#Radiodef,你结束了两个小时的拉发会议。甜! – user2573222 2014-09-22 22:49:03