排序集合的对象 (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对象实施排序:

    1. By using Comparable
    2. By using Comparator
  • When we call Collections.sort(). It sorts an object based on natural sorting or default sorting(i.e Ascending order) that's 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) that's 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:\Programs>javac TreeSetClass.java
E:\Programs>java 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:\Programs>javac TreeSetClass.java
E:\Programs>java 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

如何在Java中对Collection对象进行排序?相关推荐

  1. java对象如何保存日期_如何在Java中的日期对象中存储和检索毫秒?

    我正在制作一个读取字幕(.srt)文件的基本Java程序,我想将每次存储为Date对象.我真的只需要跟踪小时,分钟,秒和毫秒(到3位数).我想我能用这个存储它: String start = &quo ...

  2. 初始化list java_如何在Java中初始化List 对象?

    如果你检查List http://docs.oracle.com/javase/6/docs/api/java/util/List.html的API,你会注意到: Interface List 作为一 ...

  3. java 两个字段排序_如何在Java中按两个字段排序?

    使用Java 8流方法..//Creates and sorts a stream (does not sort the original list) persons.stream().sorted( ...

  4. java 两个字段排序,如何在Java中按两个字段排序?

    I have array of objects person (int age; String name;). How can I sort this array alphabetically by ...

  5. 如何在Java中初始化List <String>对象?

    本文翻译自:How to initialize List object in Java? I can not initialize a List as in the following code: 我 ...

  6. 如何在Java中比较日期? [重复]

    本文翻译自:How to compare dates in Java? [duplicate] This question already has answers here : 这个问题已经在这里有了 ...

  7. 如何在java中调用js方法

    [java] view plain copy/* * 加载脚本引擎,并在java中调用js方法 */ public void test2() { ScriptEngineManager manager ...

  8. java 中覆 写tostring_如何在Java中正确覆盖toString()?

    如何在Java中正确覆盖toString()? 听起来有点愚蠢,但我需要帮助我的toString()方法,这是非常irking. 我尝试在网上查找,因为toString是搞砸了,"没有找到K ...

  9. (转)java中对集合对象list的几种循环访问总结

    Java集合的Stack.Queue.Map的遍历 在集合操作中,常常离不开对集合的遍历,对集合遍历一般来说一个foreach就搞定了,但是,对于Stack.Queue.Map类型的遍历,还是有一些讲 ...

最新文章

  1. 随机生成50个字段的elasticsearch的测试程序输入
  2. Java集合及concurrent并发包总结(转)
  3. 【知识小课堂】mongodb 之 特殊集合及索引
  4. finalize方法作用
  5. cf1526E. Oolimry and Suffix Array(未解决)
  6. DMA及cache一致性的学习心得
  7. PHP删除数组中的空值
  8. java 求向量的均值,标准数组——向量
  9. 当罗密欧遇到朱丽叶... ...当指针遇到数组
  10. DRDS 柔性事务漫谈
  11. 超定方程组的经典Gram-Schmidt正交化解法
  12. Mike and Cellphone
  13. 支付宝APP支付IOS手机端java后台版
  14. 能耗分项计量监测系统在某大型公建中的应用
  15. java下的Http多线程下载与断点续传分析【转自酷勤网】
  16. 2020年Gartner新兴技术成熟度曲线,AI持续增强
  17. 龙芯平台常用OS 及下载链接
  18. 如何给PDF文件去水印,10秒轻松搞定
  19. 酒店在线订房小程序开发解决方案
  20. CF 770 B(奇偶性), C(规律)

热门文章

  1. 计算机文化基础重点知识归纳,计算机文化基础_第二章重点知识总结(考试必备!!!)...
  2. python 类静态属性_如何从Python中的类中引用静态属性?
  3. JDK源码解析之 java.lang.Exception
  4. UVA1586 ​​​​​​​ Molar mass
  5. 搞懂toString()与valueOf()的区别
  6. Android5.0新控件
  7. shell脚本:批量修改文件名(文件名中添加字符)
  8. ajax的访问 WebService 的方法
  9. php-对银行卡号做掩码处理
  10. SublimeText2使用笔记