Java中的 TreeSet 比较器()方法及示例
TreeSet 是Java中 SortedSet 接口的最重要实现之一,它使用树进行存储。无论是否提供显式比较器,元素的顺序都由一个集合使用它们的自然顺序来维护。这必须与equals一致才能正确实现Set接口。
在Java.util.TreeSet 中存在的 compare () 方法共享一个重要的函数,即设置和返回可用于对 TreeSet 中的元素进行排序的比较器。如果集合遵循元素的自然排序模式,则该方法返回 Null 值。
句法:
comp_set = (TreeSet)tree_set.comparator()
参数:该方法不带任何参数。
返回值:比较器集用于按特定顺序对集合中的元素进行排序。如果集合遵循默认或自然排序模式,则返回 Null 值。
在这里,我们将在下面提出两个示例,之前我们将使用元素的自然顺序,稍后使用特定的比较器来更好地理解它。
示例 1:使用元素的自然排序
Java
// Java Program to illustrate the use of comparator() method
// While using the natural ordering of the elements
// Importing utility classes
import java.util.*;
// Main class
// TreeSet Demo class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty TreeSet of integer type
TreeSet tree_set = new TreeSet();
// Adding elements to the set
// using add() method
tree_set.add(20);
tree_set.add(24);
tree_set.add(30);
tree_set.add(35);
tree_set.add(45);
tree_set.add(50);
// Printing elements inside TreeSet object
System.out.println("Tree Set values are: "
+ tree_set);
// Creating a comparator
Comparator comp = tree_set.comparator();
// Print and display the comparator values
System.out.println("Since the Comparator value is: "
+ comp);
// Display message only
System.out.println("it follows natural ordering");
}
}
Java
// Java code to illustrate the use of comparator()
// While using a specific comparator
// Importing Comparator and TreeSet classes
// from java.util package
import java.util.Comparator;
import java.util.TreeSet;
// Class 1
// Helper class
class Helper implements Comparator {
// Method
// To compare two strings
public int compare(String str1, String str2)
{
String first_Str;
String second_Str;
first_Str = str1;
second_Str = str2;
// using compareTo() to ensure
return second_Str.compareTo(first_Str);
}
}
// Main class
// TreeSetDemo class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty TreeSet of string type
TreeSet tree_set = new TreeSet();
// Adding elements to our TreeSet object
// using add() method
tree_set.add("G");
tree_set.add("E");
tree_set.add("E");
tree_set.add("K");
tree_set.add("S");
tree_set.add("4");
// Printing elements in set before using comparator
System.out.println("Set before using the comparator: " + tree_set);
// Again creating an empty TreeSet of string type
// with reference to Helper class
TreeSet tree_set1 = new TreeSet(new Helper());
// Adding elements to our TreeSet object
// using add() method
tree_set1.add("G");
tree_set1.add("E");
tree_set1.add("E");
tree_set1.add("K");
tree_set1.add("S");
tree_set1.add("4");
// Printing elements in set before using comparator
System.out.println("The elements sorted in descending order:" + tree_set1);
}
}
输出:
Tree Set values are: [20, 24, 30, 35, 45, 50]
Since the Comparator value is: null
it follows natural ordering
示例 2:使用特定的比较器
Java
// Java code to illustrate the use of comparator()
// While using a specific comparator
// Importing Comparator and TreeSet classes
// from java.util package
import java.util.Comparator;
import java.util.TreeSet;
// Class 1
// Helper class
class Helper implements Comparator {
// Method
// To compare two strings
public int compare(String str1, String str2)
{
String first_Str;
String second_Str;
first_Str = str1;
second_Str = str2;
// using compareTo() to ensure
return second_Str.compareTo(first_Str);
}
}
// Main class
// TreeSetDemo class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty TreeSet of string type
TreeSet tree_set = new TreeSet();
// Adding elements to our TreeSet object
// using add() method
tree_set.add("G");
tree_set.add("E");
tree_set.add("E");
tree_set.add("K");
tree_set.add("S");
tree_set.add("4");
// Printing elements in set before using comparator
System.out.println("Set before using the comparator: " + tree_set);
// Again creating an empty TreeSet of string type
// with reference to Helper class
TreeSet tree_set1 = new TreeSet(new Helper());
// Adding elements to our TreeSet object
// using add() method
tree_set1.add("G");
tree_set1.add("E");
tree_set1.add("E");
tree_set1.add("K");
tree_set1.add("S");
tree_set1.add("4");
// Printing elements in set before using comparator
System.out.println("The elements sorted in descending order:" + tree_set1);
}
}
输出
Set before using the comparator: [4, E, G, K, S]
The elements sorted in descending order:[S, K, G, E, 4]