Collection 与 Collections 的区别?

版权声明:努力去改变!!! https://blog.csdn.net/lq1759336950/article/details/88898448

collection:它属于集合框架中的一个接口,并且是顶
层接口。而 Collectios 则是在集合框架中给我们提供的一个工具类,它提供了一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。这里我们重点研究下排序方法。
Collectios 类提供了两个静态重载方法用于排序,如下:
java.util.Collections.sort(java.util.List)
java.util.Collections.sort(java.util.List, java.util.Comparator)
同时还提供了求最大值最小值的方法:
java.util.Collections.max(java.util.List)
java.util.Collections.min(java.util.List)
注意::所谓给对象排序,就是按照其中的一个属性给对象排序
方法一:
第一步:让要排序的对象实现 Comparable 接口

public class Student implements Comparable<Student> {
			private String name;
			private int age;
			public int compareTo(Student o) {
			//排序规则在此定义,现在我们用age排序比较的是age
			//如果使用其他属性排序,则在这比较
					return age - o.getAge();
			}
		}
	第二步:声明对象,添加排序代码。
	

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestSort {
    public static void main(String[] args) {
	    Student stu1 = new Student("zhangsan", 25);
	    Student stu2 = new Student("lisi", 18);
	    Student stu3 = new Student("wangba", 20);
	    Student stu4 = new Student("tiaqi", 17);
	    Student stu5 = new Student("zhaoliui", 30);
	    List<Student> stus = new ArrayList<Student>();
	    stus.add(stu1);
	    stus.add(stu2);
	    stus.add(stu3);
	    stus.add(stu4);
	    stus.add(stu5);
	    System.out.println("原始数据:");
	    for (Student stu : stus) {
	   		 System.out.println(stu);
 	   }
	    System.out.println("进行排序.........................");
	    Collections.sort(stus);
	    //传入Collections.reverseOrder()可以实现元素倒序
	    Collections.sort(stus, Collections.reverseOrder());
	    System.out.println("排序后的数据:");
	    for (Student stu : stus) {
	   		 System.out.println(stu);
	    }
	    Student maxStu = Collections.max(stus);
	    System.out.println("max = " + maxStu);
	    Student minStu = Collections.min(stus);
	    System.out.println("min = " + minStu);
    }
}

以上的代码缺陷有两个,第一:就是必须修改要排序的 Student 类,也就说要让某个对象进行排序,必须修改类的源文件让他实现 Comparable 接口,对系统有侵入性,代码高耦合。第二:如果在某一个业务中我们需要按年龄排序,另一个业务中我们需要按体重排序。但是上面 Comparable 接口只能按照一种规则排序。
方法二:
第一:实现 COMPARATOR 接口

Comparator 接口在 java.util 包中,使用的时候需要导入:
import java.util.Comparator;
public class ComparatorStudent implements Comparator<Student> {
    @Override
    public int compare(Student stu1, Student stu2) {
	    //根据不同的业务需求给不同的属性做比较
	    return stu2.getAge() - stu1.getAge();
    }
}

第二:在排序时,需要传入对应的实现类:

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    public class TestSort {
	        public static void main(String[] args) {
			    //与上例相同,代码省略
			   System.out.println("进行排序.........................");
			   ComparatorStudent comparatorStudent = new ComparatorStudent();
			   //根据不同的排序规则创建不同的Comparator实现类
			   Collections.sort(stus, comparatorStudent);
			    System.out.println("排序后的数据:");
			    for (Student stu : stus) {
			 		   System.out.println(stu);
	   			 }
	   		}
}

优点:不需要修改源类文件,从新编写一个类,对 Student 源文件没有侵入性,低耦合。
比如现在在 A 业务中我们需要按照年龄排序,在 B 业务中修要根据体重排序,那么只需要编
写两个 Comparator 实现类:按照年龄排序的与按照体重排序的,在调用 Collections 的
sort 方式时传入相应排序实现即可。

猜你喜欢

转载自blog.csdn.net/lq1759336950/article/details/88898448
今日推荐