h5网站制作视频,网站平台推广方案,网页设计的首页版式,苏州园区排序集合的对象 (Sorting objects of the Collection) This concept is related to sorting and here we will see how to sort objects on the Collection? 这个概念与排序有关#xff0c;在这里我们将看到如何对Collection上的对象进行排序#xff1f; In java, we have u… 排序集合的对象 (Sorting objects of the Collection) This concept is related to sorting and here we will see how to sort objects on the Collection? 这个概念与排序有关在这里我们将看到如何对Collection上的对象进行排序 In java, we have utility class Collections which provide various methods to perform various task and one of the methods of Collection class is related to sorting like sort(). 在Java中我们有实用程序类Collections它提供了执行各种任务的各种方法并且Collection类的方法之一与sort()之类的排序有关。 We can implement sorting on Collection object in two ways: 我们可以通过两种方式对Collection对象实施排序 By using ComparableBy using ComparatorWhen we call Collections.sort(). It sorts an object based on natural sorting or default sorting(i.e Ascending order) thats is specified in compareTo() method. 当我们调用Collections.sort()时 。 它根据compareTo()方法中指定的自然排序或默认排序(即升序)对对象进行排序。 When we call Collections.sort(Comparator). It sorts an object based on customized sorting (i.e Ascending order or Descending order) thats is specified in compare() method of Comparator. 当我们调用Collections.sort(Comparator)时 。 它根据在Comparator的compare()方法中指定的自定义排序(即升序或降序)对对象进行排序。 We will see the sorting ways one by one... 我们将一一看到排序方式... 1)通过使用比较器 (1) By using Comparator) If we pass the Comparator object in Collection class constructor then our compare() method will be executed. 如果我们在Collection类构造函数中传递Comparator对象则将执行compare()方法。 When we want customize sorting then we should go for Comparator. 当我们想要自定义排序时我们应该选择比较器。 It is possible to implement customized sorting by using Comparator interface. (Customized sorting means that according to our need whether it is ascending or descending). 使用Comparator接口可以实现自定义排序。 (自定义排序意味着根据我们的需要是升序还是降序)。 Example: 例 import java.util.*;
class TreeSetClass {
public static void main(String[] args) {
// Here we are passing Comparator object in Collection
// class constructor for custoize sorting
TreeSet ts new TreeSet(new CustomizeSorting());
// adding elements to TreeSet
ts.add(10);
ts.add(40);
ts.add(30);
ts.add(20);
// Customized Sorted List
System.out.println(Customize sorting : ts);
}
}
// Here we are implementing Comparator interface
class CustomizeSorting implements Comparator {
// Here we are overrding compare() method of Comparator
public int compare(Object obj1, Object obj2) {
Integer i1 (Integer) obj1;
Integer i2 (Integer) obj2;
return -i1.compareTo(i2);
}
}
Output 输出量 E:\Programsjavac TreeSetClass.java
E:\Programsjava TreeSetClass
Customize sorting :[40, 30, 20, 10]
2)使用可比接口 (2) By using Comparable interface) For predefined Comparable classes default natural sorting is already available. 对于预定义的可比较类默认的自然排序已可用。 For predefined Non-Comparable classes default natural sorting is not already available. 对于预定义的“不可比较”类默认自然排序尚不可用。 For our customized classes to define natural sorting then we should go for Comparable. 为了让我们的自定义类定义自然排序我们应该选择Comparable。 In case of default natural sorting compulsory object should be homogenous and Comparable otherwise we will get CCE (ClassCastException). 在默认情况下自然排序强制对象应该是同质且可比较的否则我们将获得CCE(ClassCastException)。 Example: 例 import java.util.*;
class TreeSetClass {
public static void main(String[] args) {
Student s1 new Student(10);
Student s2 new Student(30);
Student s3 new Student(70);
Student s4 new Student(20);
// Here we are not passing Comparator object in Collection
// class constructor for default sorting
TreeSet ts new TreeSet();
// adding elements to TreeSet
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
// Customized Sorted List
System.out.println(Default sorting : ts);
}
}
// Here we are implementing Comparable interface
class Student implements Comparable {
int code;
Student(int code) {
this.code code;
}
public String toString() {
return Code - code;
}
// Here we are overrding compare() method of Comparable interface
public int compareTo(Object obj) {
int code1 this.code;
Student intermediate (Student) obj;
int code2 intermediate.code;
if (code1 code2)
return -1;
else if (code1 code2)
return 1;
else
return 0;
}
}
Output 输出量 E:\Programsjavac TreeSetClass.java
E:\Programsjava TreeSetClass
Default sorting :[ Code - 10, Code - 20, Code - 30, Code - 70]
翻译自: https://www.includehelp.com/java/how-to-sort-objects-of-the-collection-in-java.aspx