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
,location
、description
のEvent
クラスを作成します。
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));
チェックアウト[スタックドキュメント](のhttp:// stackoverflowの.com/documentation/java/90/collections/12413/usage-of-hashmap) –
ありがとう、私もそれを見つけました。 _eventCategory_の値である_eventMap_に別のイベントを追加するにはどうすればよいですか? ** categoryMap(eventCategory、eventMap.put(eventTitle、singleEventMap))**?これは動作しないためです。 申し訳ありませんが、私の質問がばかだと。私は簡単な解決策を見ることができないようです。 – Laura