62,623
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args)
{
Map<String, Map<String, List<String>>> maps = new HashMap<String, Map<String, List<String>>>();
Map<String, List<String>> m1 = new HashMap<String, List<String>>();
List<String> la = new ArrayList<>();
la.add("李世民");
la.add("李建成");
la.add("平西王");
List<String> lb = new ArrayList<>();
lb.add("秦叔宝");
lb.add("单雄信");
lb.add("平西王");
m1.put("早发白帝城", la);
m1.put("送孟浩然之广陵", lb);
Map<String, List<String>> m2 = new HashMap<String, List<String>>();
List<String> lc = new ArrayList<>();
lc.add("赵匡胤");
lc.add("朱元璋");
lc.add("李鸿章");
List<String> ld = new ArrayList<>();
ld.add("平西王");
ld.add("平南王");
ld.add("李建成");
m2.put("闻官军收河南河北", lc);
m2.put("石壕吏", ld);
maps.put("李白", m1);
maps.put("杜甫", m2);
System.out.println(maps);
}
public class Work {
//name of the work, e.g. 早发白帝城
private String workName;
//gets and sets
}
那么我可以写一个class 叫做诗人, 来满足你的描述
public class Poet{
// Name of poet
private String poetName;
//the work created by the poet
List<Work> authoredWork;
}
但同时, 诗人也可以作为一个作品的属性, 那么把诗人定义在work class里也是可以的, 这样的话就比较符合你下边想要的数据结构。
public class Work {
//name of the work, e.g. 早发白帝城
private String workName;
private Poet author;
//gets and sets
}
而读者和诗的关系并不是那么密切, 所以最好的办法应该是用observer pattern 让读者可以自由subscribe要读什么诗。。
这样可以保证代码有最低的耦合度。