好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java常用工具类—集合排序

一、集合排序概述

1、主要内容

集合中的基本数据类型排序 集合中的字符串排序 comparator接口 comparable接口

回顾:

?

1

2

3

//数组的排序

int [] arr= { 2 , 3 , 4 , 5 , 2 , 1 };

arrays.sort(arr);

2、集合排序方法

使用collections类的sort(list list)方法 sort(list list)是根据元素的自然顺序对指定列表按升序进行排序。

二、对基本数据类型和字符串类型进行排序

1、对基本数据类型排序

list中只能存放对象,要想存放基本数据类型的话,泛型中只能写其对应的包装类。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

import java.util.arraylist;

import java.util.collections;

import java.util.list;

 

public class intsort {

 

     public static void main(string[] args) {

         //对存储在list中的整形数据进行排序

         list<integer> list = new arraylist<integer>();

         list.add( 5 );

         list.add( 9 );

         list.add( 3 );

         list.add( 1 );

         system.out.println( "排序前:" );

         for ( int n: list) {

             system.out.print(n + " " );

         }

         system.out.println();

         //对list中的数据进行排序

         collections.sort(list);

         system.out.println( "排序后:" );

         for ( int n: list) {

             system.out.print(n + " " );

         }

     }

    

}

2、对字符串排序

集合中字符串的排序后其实是按字符顺序,ascii值顺序进行排序的

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

import java.util.arraylist;

import java.util.collections;

import java.util.list;

 

public class stringsort {

 

     public static void main(string[] args) {

         //对存放在list中的字符串进行排序。

         list<string> list = new arraylist<string>();

         list.add( "orange" );

         list.add( "blue" );

         list.add( "yellow" );

         list.add( "gray" );

         system.out.println( "排序前: " );

         for (string str: list) {

             system.out.print(str + " " );

         }

         system.out.println();

         //对list中的数据进行排序

         collections.sort(list);

         system.out.println( "排序后: " );

         for (string str: list) {

             system.out.print(str + " " );

         }

         //排序后其实是按字母顺序

     }

 

}

三、comparator接口

1、问题场景:

自定义的类如何排序?

2、解决方法:

使用comparable或comparator接口

3、comparator接口介绍

强行对某个对象进行整体排序的比较函数 可以将comparator传递给sort方法(如collections.sort或 arrays.sort) 包含方法int compare(t o1, t o2) 比较用来排序的两个参数  –如果o1<o2,返回负整数   –如果o1==o2,返回0  –如果o1>o2,返回正整数 包含方法boolean equals(object obj)指示某个其他对象是否[等于]此comparator。此方法可以被object类中的equals方法覆盖,不必重写,也就是说,我们在comparator接口中只需要重写compare这个方法。

4、对宠物猫进行排序

对自定义的类按照一定的规则进行排序:
(1)对宠物猫按名字进行排序
string类型具有compareto()方法

例:

?

1

int n = name1.conpareto(name2);

如果name1<name2时,那么n是负整数,相等则为0,否则为正整数。
name1与name2位置调换下,则会实现倒序的排序。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

import java.util测试数据parator;

//按名字进行升序排序的比较器

public class namecomparator implements comparator<cat> {

 

     @override

     public int compare(cat o1, cat o2) {

         //按名字升序排序

         string name1 = o1.getname();

         string name2 = o2.getname();

         int n = name1测试数据pareto(name2);

         return n;

     }

    

}

语法格式:

?

1

collections.sort(catlist, new namecomparator());

(2)对宠物猫按年龄降序排序

调用compare方法大于0,就把前一个数和后一个数交换,也就是把大的数放后面了,即所谓的升序了。如果第二个参数与第一个参数调换顺序,也就是降序了。 按int类型比较,直接返回减法后的差值。

?

1

2

3

4

5

6

7

8

9

10

11

12

import java.util测试数据parator;

//按年龄进行降序排序的比较器

public class agecomparator implements comparator<cat> {

 

     @override

     public int compare(cat o1, cat o2) {

         int age1 = o1.getmonth();

         int age2 = o2.getmonth();

         return age2 - age1;

     }

 

}

 语法格式:

?

1

collections.sort(catlist, new agecomparator());

四、comparable接口

1、comparable概述

comparable接口位于java.lang包下面 此接口强行对实现他的每个类的对象进行整体排序 这种排序被称为类的自然排序,类的compareto方法被称为它的自然比较方法 对于集合,通过调用collection.sort方法进行排序 对于数组,通过调用arrays.sort方法进行排序 int compareto(t o)方法:该对象小于,等于或大于指定对象,则分别返回负整数,0,或正整数

2、对商品价格进行降序排序案例

(1)操作步骤

对待排序的类进行接口实现 重写compareto方法 直接调用collections.sort()方法。

(2)代码实现
①商品类的定义

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

public class goods implements comparable<goods> { //实现comparable接口,利用泛型限定比较的类型

     private string id; //商品编号

     private string name; //商品名称

     private double price; //商品价格

     //构造方法

     public goods() {

        

     }

     public goods(string id, string name, double price) {

         this .setid(id);

         this .setname(name);

         this .setprice(price);

     }

    

     //getter和setter方法

     public string getid() {

         return id;

     }

     public void setid(string id) {

         this .id = id;

     }

     public string getname() {

         return name;

     }

     public void setname(string name) {

         this .name = name;

     }

     public double getprice() {

         return price;

     }

     public void setprice( double price) {

         this .price = price;

     }

    

    

     @override

     public string tostring() {

         return "商品编号:" + id + ",商品名称:" + name + ",商品价格:" + price;

     }

 

     @override

     public int compareto(goods o) {  //重写compareto方法。

         //取出商品价格

         double price1 = this .getprice();

         double price2 = o.getprice();

         int n = new double (price2 - price1).intvalue();  //double类型的差值转为int

         return n;

     }

②排序步骤

?

1

collections.sort(goodslist);

五、集合排序总结

comparator和comparable接口的区别

1、comparator:

①位于java.util包
②在要比较的类的外部实现该接口
③调用sort方法时,要指定comparator的实现类
使用顺序:

实现要排序的接口 实现comparator接口 测试

2、comparable

①位于java.lang包
②在要比较的类上实现该接口
③调用sort方法时,只需指定集合名即可
使用顺序:

定义要比较的类,并实现comparable接口
测试

以上所述是小编给大家介绍的java集合排序详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

原文链接:https://blog.csdn.net/zhty88668/article/details/88655070

查看更多关于Java常用工具类—集合排序的详细内容...

  阅读:18次