2017-01-04 5 views
-1

大きなHashMapでWebサイト(カテゴリ、そのカテゴリの複数のイベント、特定のイベントに関する情報)からHTMLとして取得したイベントに関する情報を注文したいと思います。Java HashMapはPython辞書のように使用されています

HashMap categoryMap = new HashMap(); 
HashMap eventMap = new HashMap(); 
HashMap singleEventMap = new HashMap(); 
categoryMap.put(eventCategory, eventMap); 
eventMap.put(eventTitle, singleEventMap); 
singleEventMap.put("starttime", eventTime); 
singleEventMap.put("location", eventLocation); 
singleEventMap.put("description", eventDescription); 

I`mはdictonariesをPYTHONに使用されると私はカテゴリまたはどのように私はJavaで格納された情報にアクセスすることができますへの別のイベントを追加する方法を、見つけることができません。 誰かが私にコードの例や類似の問題や良い説明のリンクを与えることができたらうれしいです。

+0

チェックアウト[スタックドキュメント](のhttp:// stackoverflowの.com/documentation/java/90/collections/12413/usage-of-hashmap) –

+0

ありがとう、私もそれを見つけました。 _eventCategory_の値である_eventMap_に別のイベントを追加するにはどうすればよいですか? ** categoryMap(eventCategory、eventMap.put(eventTitle、singleEventMap))**?これは動作しないためです。 申し訳ありませんが、私の質問がばかだと。私は簡単な解決策を見ることができないようです。 – Laura

答えて

1

1)ジェネリックタイプを使用しないでください。

常に型引数を指定します。 program to the interfaceも指定してください。例えば。

Map<String, Map<String, Map<String, Object>>> categoryMap = new HashMap<>(); 
Map<String, Map<String, Object>> eventMap = new HashMap<>(); 
Map<String, Object> singleEventMap = new HashMap<>(); 

2)Javaはオブジェクト指向言語です。

など。フィールドがstarttime,locationdescriptionEventクラスを作成します。

public class Event { 
    private final LocalDateTime starttime; 
    private final String  location; 
    private final String  description; 
    public Event(LocalDateTime starttime, String location, String description) { 
     this.starttime = starttime; 
     this.location = location; 
     this.description = description; 
    } 
    public LocalDateTime getStarttime() { 
     return this.starttime; 
    } 
    public String getLocation() { 
     return this.location; 
    } 
    public String getDescription() { 
     return this.description; 
    } 
} 

は、次に使用:

Map<String, Map<String, Event>> categoryMap = new HashMap<>(); 
Map<String, Event> eventMap = new HashMap<>(); 

3)別のイベントを追加するには:

singleEventMapの別のインスタンスを作成してプロパティを追加し、​​に追加します。

あなたの道:

HashMap categoryMap = new HashMap(); 
HashMap eventMap = new HashMap(); 
categoryMap.put(eventCategory, eventMap); 

Map singleEventMap = new HashMap(); 
eventMap.put(eventTitle1, singleEventMap); 
singleEventMap.put("starttime", starttime1); 
singleEventMap.put("location", location1); 
singleEventMap.put("description", description1); 

singleEventMap = new HashMap(); 
eventMap.put(eventTitle2, singleEventMap); 
singleEventMap.put("starttime", starttime2); 
singleEventMap.put("location", location2); 
singleEventMap.put("description", description2); 

Javaの道:

Map<String, Map<String, Event>> categoryMap = new HashMap<>(); 
Map<String, Event> eventMap = new HashMap<>(); 
categoryMap.put(eventCategory, eventMap); 

eventMap.put(eventTitle1, new Event(starttime1, location1, description1)); 

eventMap.put(eventTitle2, new Event(starttime2, location2, description2)); 

それとも、彼らはさまざまなカテゴリを持っている場合:

Map<String, Map<String, Event>> categoryMap = new HashMap<>(); 

Map<String, Event> eventMap1 = new HashMap<>(); 
categoryMap.put(eventCategory1, eventMap1); 
eventMap1.put(eventTitle1, new Event(starttime1, location1, description1)); 

Map<String, Event> eventMap2 = new HashMap<>(); 
categoryMap.put(eventCategory2, eventMap2); 
eventMap2.put(eventTitle2, new Event(starttime2, location2, description2)); 
+1

あなたは私よりも1秒早くでした:)私は、クラスマップを 'addEvent'メソッドと' getEvent'メソッドで独自のクラス( 'Catalog')にカプセル化することもお勧めします。また、 'category'と' title'が 'Event'のフィールドになるのは意味があるようです。 –

+0

ありがとうございました!私はすでにイベントクラスを作ったことを完全に忘れてしまった!既に推測したように、私はJavaの専門家ではないので、あなたの詳細な説明に本当に感謝しています。 – Laura

関連する問題