答案 0 :(得分:1)
HashMap
和HashSet
不是排序的集合,因此没有构造函数可以使用Comparator
。相反,您必须使用TreeSet
或TreeMap
,其中包含一个重载的构造函数才能使用Comparator
。因此,应将此代码更正。
Map<Student, String> empMap = new TreeMap<>(new MyCustComparator());
Set<Student> set = new TreeSet<>(new MyCustComparator());