这是我的代码:
/* Returns a Map that stores a contact name as a key and a list of messages from that contact
as a value. If a message has no associated contact, it should not appear in the Map. Must
not change messages field. Must call filter with an anonymous inner class in the method body. */
public Map<String, List<Message>> sortMessagesByContact() {
Map<String, List<Message>> map = new HashMap<>();
List<Message> filtered = new ArrayList<>();
Predicate<Message> p = new Predicate<>() {
@Override
public boolean test(Message t) {
return t.getContact().isPresent();
}
}; for (Message mg : messages) {
if (p.test(mg)) {
map.put(mg.getContact().get(), messages);
}
}
return map;
}
这是我到目前为止所得到的。但我无法想出一种方法来将来自该联系人的消息列表作为值。顺便说一句,我应该在这里使用匿名内部类
例如,当打印带有四条消息的地图时,
我应该得到这样的东西:
James = [bakjd],[adjlfaj],[daklfja], Howard = [dajfkla]
宝慕林4294392
桃花长相依
相关分类