Vectors实现了List接口,用于创建动态数组。大小不固定且可以根据我们的需求增长的数组被称为动态数组。Comparator是‘java.util’包中可用的一个接口。
排序意味着按升序或降序重新排列给定列表或数组的元素。在本文中,我们将创建一个向量,然后尝试使用比较器按降序对其元素进行排序。
按降序排列Java向量的程序
Comparator
正如其名称所示,它用于比较某些东西。在Java中,Comparator是一个接口,用于对自定义对象进行排序。我们可以在其内置方法“compare()”中编写自己的逻辑来对指定的对象进行排序。该方法接受两个对象作为参数,然后返回一个整数值。通过这个整数值,Comparator决定哪个对象更大。
Syntax
Comparator nameOfComparator = new Comparator() {
compare( type object1, type object1 ) {
// logic for comparison
}
};
登录后复制
在像‘Collection.sort()’这样的方法中,nameOfComparator是用于排序操作的参数。
Collections.sort() method
The class ‘Collections’ of the Collection Interface provides a static method named ‘Collections.sort()’ that can sort elements of specified collections like ArrayList or LinkedList. It is available in ‘java.util’ package.
Syntax
Collections.sort( nameOfcollection, ComparatorObject );
登录后复制
Collections.reverseOrder()
It returns the comparator in reverse order.
Example 1
In the following example, we will define a vector named ‘vectlist’ and store a few objects in it by using the ‘add()’ method. Then, use the Comparator object and ‘Collection.sort()’ method to sort the vector in descending order.
import java.util.*;
public class VectClass {
public static void main(String args[]) {
// Creation of vector
Vector 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
登录后复制
Example 2
In this example, first, we will create a Comparator and inside it, we define our logic in ‘compare()’ method to sort the vector objects in descending order. The logic here states that take two objects at the same time and compare them using the if-else block. If first object is greater than second return -1 otherwise 1. Then, we pass the object of comparator to ‘Collection.sort()’ for sorting operation.
import java.util.*;
public class VectClass {
public static void main(String args[]) {
// Using comparator interface for sorting
Comparator comp = new Comparator() {
// 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 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
登录后复制
结论
This article has explained the implementation of the Comparator Interface and also we discovered the use of a few inbuilt methods such as ‘compareTo()’, ‘Collection.sort()’ and ‘Collections.reverseOrder()’.
以上就是使用比较器将Java向量按降序排序的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!