1

Based on the answer: https://stackoverflow.com/a/47693998/8760211

Following Compile Error occurs, when I try to sort a list with elements of Type Student from within the class Invoker. Student is an innerclass of class University:

Type mismatch: cannot convert from ToIntFunction<University.Student> to ToIntFunction<? super T>

Code:

public class University implements Serializable {
    private List<Student> values = new ArrayList<Student>();

    public static final class Student implements Serializable {
        private int id;
        private String name;
        private String location;
        private Date date;

        Student(int id, String name, String location, Date date) {
            this.id = id;
            this.name = name;
            this.location = location;
            this.date = date;
        }

        // Problem occurs even with getters!!
}


public class Invoker implements Serializable {
    public static void main(String[] args) {
        List<University.Student> students = new ArrayList<>();

        Map<String, Integer> locationOrder = students.stream().collect(HashMap::new,
                (m, s) -> m.putIfAbsent(s.getName(), m.size()),
                (m1, m2) -> m2.keySet().forEach(l -> m1.putIfAbsent(l, m1.size())));

        students.sort(Comparator.comparingInt((University.Student s) -> locationOrder.get(s.name)).thenComparing(s -> s.id));        
    }
}

The following code snippet is marked in IDE as cause of the Compiler error: comparingInt((University.Student s) -> locationOrder.get(s.name)

If I move the innerclass into the class Invoker everything works fine!!

Any help would be much appreciated. Thanks in advance.

0

2 Answers 2

2

This is a strange error to get, but the reason is just that s.name is not accessible in the Invoker: it's private. Neither is s.id. So you should add getter methods to Student like

public String getName() {
    return name;
}

and use them instead. Enclosing classes can access the inner classes' private members which is why it works if moved to Invoker.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi Alexey, Many thanks for your reply. I have the getters in the class Student. But even with those getters I get the compiler error!! It's really very strange.
I tested by copying your code, making classes non-public (so they can all go in one file) and adding the getters, and it works: ideone.com/cQz7R5. Please double-check that your code is as in the question (different code would explain the error much better).
Hi Alexey, thanks for your reply. Yes, if all classes are in one file it works! But I have to seperated the classes in different files and so then I get the described error!!
Then something really weird is going on. How exactly are you compiling and running the code?
Hi Alexey, I'm using eclipse with JDK 1.8. I will try it today with another IDE and then give you feedback.
|
1

Try changing it to this

students.sort(Comparator.<University.Student>comparingInt(s -> locationOrder.get(s.name)).thenComparing(s -> s.id));        

3 Comments

Hi Leo, thanks for your reply. the compile error is solved but the List is sorted by id, so the grouping disappears!! Very odd behavior.
Well that depends on locationOrder. Is it a map or something?
Hi Leo, locationOrder is a Map<String, Integer>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.