This is what I have so far
Map<String, String> courses = new HashMap();
courses.put("Teachers","adam");
so how do I add more teachers using the same key
You can use Map<String, ArrayList<String>>
. Then add additional information by doing map.get("Teachers").add("Bob")
You can benefit from computeIfAbsent :
Map<String, List<String>> courses = new HashMap<>();
courses.computeIfAbsent("Teachers", k -> new ArrayList<>()).add("adam");
if you can use google guava, Interface Multimap<K,V>
is a good way to do this,and all opereation is simple。
To add a dependency on Guava using Maven, use the following:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
<!-- or, for Android: -->
<version>27.0.1-android</version>
</dependency>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.