2017-06-18 50 views
-5

我有一個輸入hashtable中的一些值的問題, HashMap<String, ArrayList<Integer>>必須包含名稱和相對points.I'm無法爲鍵更多的值。 輸入文件如下。#java HashMap和輸入

Marco,Matteo,Luca,Giovanni;34,35,3,35 
Marco,Anna,Chiara;1,5,6 
+4

而你的問題是... –

+0

你是什麼意思的「相對點」? – alejandrogiron

+0

只需使用一個'HashMap ' – victor

回答

1

地圖不接受重複密鑰。

你可以切換到使用一個地圖的列表(每行一個地圖):

List<Map<String, Integer>> records = ... 
Map<String, Integer> map1 = new HashMap<>(), 
    map2 = new HashMap<>(); 
map1.put("Marco", 34); 
map1.put("Matteo",35); 
//put values for Luca,Giovanni... 
records.add(map1); 
//create and add maps for other rows 

或映射一個列表/陣列(每列一個清單)

Map<String, List<Integer>> map1 = new HashMap<>() 
List<Integer> marco = ... 
marco.add(34); 
marco.add(1); 
map1.put("Marco", marco); 
//do the same for other records... 
+0

在什麼情況下,地圖列表比地圖列表更有意義? – victor

+0

不幸的是,該程序必須能夠讀取具有該結構的每個文件。我寫了這段代碼,但我不知道如何寫最後一部分它不訂閱密鑰 –

+0

{String [] name = line.split(「;」)[0] .split(「,」); \t \t \t \t \t ArrayList p = new ArrayList (); \t \t \t \t \t對(INT I = 0; I st = new ArrayList (); \t \t \t \t \t對(INT I = 0; I (); \t \t \t \t \t \t \t \t st.add(p.get(I)); \t \t \t \t \t \t \t \t h.put(名稱[I],ST); \t \t \t \t \t \t \t} –