我有一個輸入hashtable中的一些值的問題, HashMap<String, ArrayList<Integer>>
必須包含名稱和相對points.I'm無法爲鍵更多的值。 輸入文件如下。#java HashMap和輸入
Marco,Matteo,Luca,Giovanni;34,35,3,35
Marco,Anna,Chiara;1,5,6
我有一個輸入hashtable中的一些值的問題, HashMap<String, ArrayList<Integer>>
必須包含名稱和相對points.I'm無法爲鍵更多的值。 輸入文件如下。#java HashMap和輸入
Marco,Matteo,Luca,Giovanni;34,35,3,35
Marco,Anna,Chiara;1,5,6
地圖不接受重複密鑰。
你可以切換到使用一個地圖的列表(每行一個地圖):
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...
在什麼情況下,地圖列表比地圖列表更有意義? – victor
不幸的是,該程序必須能夠讀取具有該結構的每個文件。我寫了這段代碼,但我不知道如何寫最後一部分它不訂閱密鑰 –
{String [] name = line.split(「;」)[0] .split(「,」); \t \t \t \t \t ArrayList
可能提供你想要實現的更多細節。 據我所知,你想存儲多個值的密鑰?
Multimap在這種情況下很有用。 https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/Multimap.html 希望這可以幫助你!
而你的問題是... –
你是什麼意思的「相對點」? – alejandrogiron
只需使用一個'HashMap' –
victor