Java 使用Comparator将Vector按降序排序
Vector实现了List接口,并用于创建动态数组。大小不固定且可以根据需要增长的数组称为动态数组。Comparator是‘java.util’包中可用的一个接口。
排序意味着按升序或降序重新排列给定列表或数组的元素。在本文中,我们将创建一个向量,然后尝试使用Comparator按降序对其元素进行排序。
以降序排序的Java Vector程序
Comparator
顾名思义,它用于比较某些东西。在Java中,Comparator是一个用于排序自定义对象的接口。我们可以在它的内置方法‘compare()’中编写自己的逻辑来对指定的对象进行排序。此方法接受两个对象作为参数,然后返回一个整数值。通过这个整数值,Comparator决定哪个对象更大。
语法
Comparator< TypeOfComparator > nameOfComparator = new Comparator< TypeOfComparator >() {
compare( type object1, type object1 ) {
// logic for comparison
}
};
传递给类似于“Collection.sort()”的方法的 nameOfComparator 用于排序操作。
Collections.sort()方法
Collection接口的 ‘Collections’ 类提供了一个名为 ‘Collections.sort()’ 的静态方法,可以对指定集合(如ArrayList或LinkedList)的元素进行排序。它位于 ‘java.util’ 包中。
语法
Collections.sort( nameOfcollection, ComparatorObject );
Collections.reverseOrder()
它返回一个反向顺序的比较器。
示例1
在下面的示例中,我们将定义一个名为’vectlist’的向量,并使用’add()’方法将一些对象存储在其中。然后,使用比较器对象和’Collection.sort()’方法按降序对向量进行排序。
import java.util.*;
public class VectClass {
public static void main(String args[]) {
// Creation of vector
Vector<Integer> vectList = new Vector<>();
// Adding elements in the vector
vectList.add(97);
vectList.add(93);
vectList.add(95);
vectList.add(99);
vectList.add(82);
vectList.add(88);
System.out.println("Elements of the unsorted list: ");
// loop to iterate through elements
for(int i = 0; i < vectList.size(); i++ ) {
// to print the elements of the vector
System.out.print(vectList.get(i) + " ");
}
System.out.println();
// Using comparator interface for sorting
Comparator comp = Collections.reverseOrder();
Collections.sort(vectList, comp);
System.out.println("Elements of the newly sorted list: ");
// loop to iterate through elements
for(int i = 0; i < vectList.size(); i++ ) {
// to print the elements of the new vector
System.out.print(vectList.get(i) + " ");
}
}
}
输出
Note: VectClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Elements of the unsorted list:
97 93 95 99 82 88
Elements of the newly sorted list:
99 97 95 93 88 82
示例2
在这个示例中,首先,我们将创建一个比较器,内部定义了一个‘compare()’方法,用来按降序对向量对象进行排序。这里的逻辑是同时取两个对象并使用if-else块进行比较。如果第一个对象大于第二个对象,则返回-1,否则返回1。然后,我们将比较器的对象传递给‘Collection.sort()’进行排序操作。
import java.util.*;
public class VectClass {
public static void main(String args[]) {
// Using comparator interface for sorting
Comparator<Integer> comp = new Comparator<Integer>() {
// logic to sort in descending order
public int compare(Integer i, Integer j) {
if(i < j) {
return 1;
} else {
return -1;
}
}
};
// Creation of vector
Vector<Integer> vectList = new Vector<>();
// Adding elements in the vector
vectList.add(97);
vectList.add(93);
vectList.add(95);
vectList.add(99);
vectList.add(82);
vectList.add(88);
System.out.println("Elements of the unsorted list: ");
// loop to iterate through elements
for(int i = 0; i < vectList.size(); i++ ) {
// to print the elements of the vector
System.out.print(vectList.get(i) + " ");
}
System.out.println();
Collections.sort(vectList, comp); // sort using comparator
System.out.println("Elements of the newly sorted list: ");
// loop to iterate through elements
for(int i = 0; i < vectList.size(); i++ ) {
// to print the elements of the new vector
System.out.print(vectList.get(i) + " ");
}
}
}
输出
Elements of the unsorted list:
97 93 95 99 82 88
Elements of the newly sorted list:
99 97 95 93 88 82
结论
本文解释了Comparator接口的实现,并且我们还发现了一些内置方法的用法,如’compareTo()’、’Collection.sort()’和’Collections.reverseOrder()’。