У меня есть экземпляр Map<Reference, Double>
проблема в том, что ключевые объекты могут содержать ссылку на один и тот же объект, мне нужно вернуть карту того же типа "input" , но сгруппированных ключом атрибута и сохранением максимального значения.
Я пробовал использовать groupingBy
и maxBy
, но я застрял.
private void run () {
Map<Reference, Double> vote = new HashMap<>();
Student s1 = new Student(12L);
vote.put(new Reference(s1), 66.5);
vote.put(new Reference(s1), 71.71);
Student s2 = new Student(44L);
vote.put(new Reference(s2), 59.75);
vote.put(new Reference(s2), 64.00);
// I need to have a Collection of Reference objs related to the max value of the "vote" map
Collection<Reference> maxVote = vote.entrySet().stream().collect(groupingBy(Map.Entry.<Reference, Double>comparingByKey(new Comparator<Reference>() {
@Override
public int compare(Reference r1, Reference r2) {
return r1.getObjId().compareTo(r2.getObjId());
}
}), maxBy(Comparator.comparingDouble(Map.Entry::getValue))));
}
class Reference {
private final Student student;
public Reference(Student s) {
this.student = s;
}
public Long getObjId() {
return this.student.getId();
}
}
class Student {
private final Long id;
public Student (Long id) {
this.id = id;
}
public Long getId() {
return id;
}
}
У меня ошибка в maxBy
аргумент: Comparator.comparingDouble(Map.Entry::getValue)
и я не знаю, как это исправить. Есть ли способ добиться ожидаемого результата?