Given the following class, what will be the output?

Last Updated :
Discuss
Comments

Given the following class, what will be the output?

Java
import java.util.*;
class Employee implements Comparable<Employee> {
    int id;
    String name;
    Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int compareTo(Employee e) {
        return this.id - e.id;
    }
    public String toString() {
        return id + " - " + name;
    }
}
public class Test {
    public static void main(String[] args) {
        List<Employee> list = new ArrayList<>();
        list.add(new Employee(3, "Alice"));
        list.add(new Employee(1, "Bob"));
        list.add(new Employee(2, "Charlie"));
        Collections.sort(list);
        System.out.println(list);
    }
}


[3 - Alice, 1 - Bob, 2 - Charlie]


[1 - Bob, 2 - Charlie, 3 - Alice]

[3 - Alice, 2 - Charlie, 1 - Bob]

Compilation Error

Share your thoughts in the comments