2017-06-18 6 views
-5

ハッシュマップの一部の値imputに問題があります。 HashMap<String, ArrayList<Integer>>に名前と相対的なポイントが含まれている必要があります。キーに値を追加できません。 入力ファイルは次のとおりです。#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 

またはリスト/アレイ(列ごとに1個のリスト)

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 \t p.add(Integer.parseInt(line.split( ";")[1] .split((; iがname.lengthを st = new ArrayList (); \t \t \t \t \t ST = h.get(名前[i])と(; iがname.lengthを()。 \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 –

-1

のマップがあることことがありますが、マップのリスト(行ごとに1つのマップ)のいずれかを使用するように切り替えることができ

あなたが達成しようとしていることの詳細を提供してください。 私が知っている限り、複数の値を格納したいキーについて理解していますか?

このような場合は、マルチマップが便利です。 https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/Multimap.html これがうまくいけばうまくいきます。

関連する問題