简体   繁体   中英

Collections.max() Improper Arguments

I'm simply trying to find the max of a list of "Officers". The specs of the assignment I'm working on don't allow me to make the Officers comparable, so instead I'm using a Comparator to compare them.

However, the compiler is complaining about the types of my arguments. Can anyone see what's wrong? (Don't worry about the return... I haven't done that yet.)

Outside of the fragment below, officers is a List<Officer> which has been initialized.

Collections.max(officers, new Comparator<Officer>()
    {
        public int compare( Officer a, Officer b )
        {
           return -1; //will do after
        }
    }
);

Any suggestions would be appreciated!

This compiles just fine. Here's an ideone.com demo

import java.util.*;

class Officer {
}

public class Test {

    public static void main(String[] args) {

        List<Officer> officers = new ArrayList<Officer>();

        Collections.max(officers, new Comparator<Officer>()
                {
                    public int compare( Officer a, Officer b )
                    {
                       return -1; //will do after
                    }
                }
            );
    }
}

So, nothing wrong with the code you posted. Must be something else that's wrong.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM