如何更改比较器以返回Java TreeSet 中的降序?
比较器接口用于对用户定义的类的对象进行排序。比较器对象能够比较两个不同类的两个对象。 TreeSet是Java中SortedSet接口最重要的实现之一,它使用Tree进行存储。无论是否提供显式比较器,元素的顺序都由使用其自然顺序的集合维护。如果要正确实现Set 接口,这必须与 equals 一致。它也可以由在集合创建时提供的 Comparator 进行排序,具体取决于使用的构造函数。
语法:对于比较器如下:
public int compare(Object obj1, Object obj2):
方法:通过更改比较器来获取 TreeSet 中的降序。
- 使用用户定义的类
- 没有用户定义的类
方法一:使用自定义类
为了更改比较器,创建了一个用户定义的类,该类实现了 Comparator 接口,更具体地说明了 desingingComparator 其伪代码如下,以便在实现之前清晰理解。 DescingComparator()在创建时作为 TreeSet 中的参数传递,以获取相应的降序 Order 集合。
语法:传递descendingComparator()
Pseudo Code: TreeSet set = new TreeSet<>(new descendingComparator());
Pseudo Code: Approach
class descendingComparator implements Comparator {
public int compare(String i1, String i2) {
// compare using compareTo() method
return i2.compareTo(i1);
}
}
例子
Java
// Java Program to Change the Comparator to Return a
// Descending Order in Java TreeSet
// Importing all classes of
// java.util package
import java.util.*;
// User0defined class (Helper class)
// New class that implements comparator interface
class descendingComparator implements Comparator {
public int compare(Integer i1, Integer i2)
{
// compare using compareTo() method
return i2.compareTo(i1);
}
}
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create a TreeSet and pass descendingComparator()
// as made in another class as parameter
// to it to get Descending Order
TreeSet set
= new TreeSet<>(new descendingComparator());
// Adding elements to Treeset
// Custom inputs
set.add(10);
set.add(20);
set.add(30);
// Print Descending Ordered set in descending order
// as above descendComparator() usage
System.out.println("Descending Ordered set : "
+ set);
}
}
Java
// Java Program to Change the Comparator
// to Return Descending Order in Java TreeSet
// Importing all classes of
// java.utoil package
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// implements comparator interface
// to get Descending Order
// Method 2: At the time of creation of TreeSet
// Creating(defining) a TreeSet
TreeSet set = new TreeSet(
new Comparator() {
// Changing the comparator
public int compare(Integer i1, Integer i2)
{
return i2.compareTo(i1);
}
});
// Add elements to above created TreeSet
// Custom inputs
set.add(10);
set.add(20);
set.add(30);
// Print Descending Ordered TreeSet
System.out.println("Descending Ordered Treeset : "
+ set);
}
}
方法二:不使用自定义类
比较器在创建 TreeSet 时返回Java TreeSet 中的降序,而不是创建新类。
Pseudo Code: Approach
TreeSet set = new TreeSet(new Comparator()
{
public int compare(Integer i1,Integer i2)
{
// comparing using compareTo() method
return i2.compareTo(i1);
}
});
例子
Java
// Java Program to Change the Comparator
// to Return Descending Order in Java TreeSet
// Importing all classes of
// java.utoil package
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// implements comparator interface
// to get Descending Order
// Method 2: At the time of creation of TreeSet
// Creating(defining) a TreeSet
TreeSet set = new TreeSet(
new Comparator() {
// Changing the comparator
public int compare(Integer i1, Integer i2)
{
return i2.compareTo(i1);
}
});
// Add elements to above created TreeSet
// Custom inputs
set.add(10);
set.add(20);
set.add(30);
// Print Descending Ordered TreeSet
System.out.println("Descending Ordered Treeset : "
+ set);
}
}
输出
Descending Ordered set : [30, 20, 10]