Java 8 Java映射:按键分组';s属性和最大超值

Java 8 Java映射:按键分组';s属性和最大超值,java-8,java-stream,Java 8,Java Stream,我有一个Map的实例,挑战在于关键对象可能包含对同一对象的引用,我需要返回一个与“输入”类型相同但按属性键分组并保留最大值的映射 我尝试使用groupingBy和maxBy但我被卡住了 private void run () { Map<Reference, Double> vote = new HashMap<>(); Student s1 = new Student(12L); vote.put(new Reference(s1), 66.5

我有一个
Map
的实例,挑战在于关键对象可能包含对同一对象的引用,我需要返回一个与“输入”类型相同但按属性键分组并保留最大值的映射

我尝试使用
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;
    }
}
private void run(){
Map vote=newhashmap();
学生s1=新生(12升);
表决.付诸表决(新的参考文件(s1),66.5);
表决.付诸表决(新的参考文件(s1),71.71);
学生s2=新生(44L);
表决.付诸表决(新的参考文件(s2),59.75);
表决.付诸表决(新的参考文件(s2),64.00);
//我需要一个与“投票”贴图的最大值相关的参考对象集合
Collection maxVote=vote.entrySet().stream().collect(groupingBy(Map.Entry.comparingByKey)(新比较器(){
@凌驾
公共整数比较(参考r1,参考r2){
返回r1.getObjId().compareTo(r2.getObjId());
}
}),maxBy(Comparator.comparingDouble(Map.Entry::getValue));
}
课堂参考{
私立期末学生;
公众参考(学生s){
这个.student=s;
}
公共长条{
返回此.student.getId();
}
}
班级学生{
私人最终长id;
公立学生(长id){
this.id=id;
}
公共长getId(){
返回id;
}
}

我在
maxBy
参数中有一个错误:
Comparator.comparingDouble(Map.Entry::getValue)
我不知道如何修复它。有什么方法可以达到预期的结果吗?

您可以使用
收集器.toMap
来获取
Map.Entry的集合

Collection result=vote.entrySet().stream()
.collect(Collectors.toMap(a->a.getKey().getObjId(),Function.identity(),
maxBy(Comparator.comparingDouble(Map.Entry::getValue))).values();
然后再次流式处理以获取
列表

List result=vote.entrySet().stream()
.collect(Collectors.toMap(a->a.getKey().getObjId(),Function.identity(),
maxBy(Comparator.comparingDouble(Map.Entry::getValue)))
.values().stream().map(e->e.getKey()).collect(Collectors.toList());

使用您的
groupingBy
maxBy
方法:

Comparator<Entry<Reference, Double>> c = Comparator.comparing(e -> e.getValue());

        Map<Object, Optional<Entry<Reference, Double>>> map = 
                           vote.entrySet().stream()
                           .collect(
                            Collectors.groupingBy
                            (
                            e -> ((Reference) e.getKey()).getObjId(),                             
                            Collectors.maxBy(c)));

           // iterate to get the result (or invoke another stream)
        for (Entry<Object, Optional<Entry<Reference, Double>>> obj : map.entrySet()) {
            System.out.println("Student Id:" + obj.getKey() + ", " + "Max Vote:" + obj.getValue().get().getValue());
        }
List<Reference> result = vote.entrySet().stream()
        .collect(Collectors.toMap(a -> a.getKey().getObjId(), Function.identity(),
            BinaryOperator.maxBy(Comparator.comparingDouble(Map.Entry::getValue))))
        .values().stream().map(e -> e.getKey()).collect(Collectors.toList());
Comparator<Entry<Reference, Double>> c = Comparator.comparing(e -> e.getValue());

        Map<Object, Optional<Entry<Reference, Double>>> map = 
                           vote.entrySet().stream()
                           .collect(
                            Collectors.groupingBy
                            (
                            e -> ((Reference) e.getKey()).getObjId(),                             
                            Collectors.maxBy(c)));

           // iterate to get the result (or invoke another stream)
        for (Entry<Object, Optional<Entry<Reference, Double>>> obj : map.entrySet()) {
            System.out.println("Student Id:" + obj.getKey() + ", " + "Max Vote:" + obj.getValue().get().getValue());
        }
Student Id:12, Max Vote:71.71
Student Id:44, Max Vote:64.0