使用 Comparator 将 Java Vector 按降序排序
Vector 实现 List 接口,用于创建动态数组。大小不固定且可以根据需要增长的数组称为动态数组。Comparator 是 ‘java.util’ 包中提供的接口。
排序是指将给定列表或数组中的元素按升序或降序重新排列。在本文中,我们将创建一个 Vector,然后尝试使用 Comparator 将其元素按降序排序。
使用 Java 将 Vector 按降序排序的程序
Comparator
顾名思义,它用于比较某些东西。在 Java 中,Comparator 是一个接口,用于对自定义对象进行排序。我们可以在其内置的名为 ‘compare()’ 的方法中编写我们自己的逻辑来对指定的对象进行排序。此方法接受两个对象作为参数,然后返回一个整数值。通过这个整数值,Comparator 决定哪个对象更大。
语法
Comparator< TypeOfComparator > nameOfComparator = new Comparator< TypeOfComparator >() {
compare( type object1, type object1 ) {
// logic for comparison
}
};
nameOfComparator 传递给排序操作的方法,例如 ‘Collection.sort()’。
Collections.sort() 方法
Collection 接口的 ‘Collections’ 类提供了一个名为 ‘Collections.sort()’ 的静态方法,该方法可以对指定集合(如 ArrayList 或 LinkedList)的元素进行排序。它在 ‘java.util’ 包中可用。
语法
Collections.sort( nameOfcollection, ComparatorObject );
Collections.reverseOrder()
它以相反的顺序返回比较器。
示例 1
在以下示例中,我们将定义一个名为 ‘vectlist’ 的 Vector,并使用 ‘add()’ 方法在其中存储一些对象。然后,使用 Comparator 对象和 ‘Collection.sort()’ 方法将 Vector 按降序排序。
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
在此示例中,首先,我们将创建一个 Comparator,并在其中定义 ‘compare()’ 方法中的逻辑以按降序对 Vector 对象进行排序。这里的逻辑表明同时获取两个对象,并使用 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()’。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP