您必须传递 Comparator
的
实例,该对象的类实现
Comparator
。您(显然)传递的是一个类,而不是该类的对象。
record Product( String description , BigDecimal price ) { }
Comparator < Product > comparatorByPrice = Comparator.comparing ( Product :: price );
List < Product > products = List.of (
new Product ( "Banana" , new BigDecimal ( "22.22" ) ) ,
new Product ( "Apple" , new BigDecimal ( "44.44" ) ) ,
new Product ( "Orange" , new BigDecimal ( "11.11" ) ) ,
new Product ( "Papaya" , new BigDecimal ( "33.33" ) )
);
NavigableSet < Product > productsSortedByPrice = new TreeSet <> ( comparatorByPrice );
productsSortedByPrice.addAll ( products );
转储到控制台。
System.out.println ( "products = " + products );
System.out.println ( "productsSortedByPrice = " + productsSortedByPrice );