Java集合框架初识二

       在上一节中大概总结了一些集合框架的基本知识,Collection 接口、List接口、Map接口、Set接口等的基本用法,今天来总结一下Collections工具类的排序方法Sort()以及Comparable接口和Comparator接口的一些实现

  数组中由Arrays工具类提供了一系列对数组的操作,而集合中由Collections工具类提供的静态方法对集合进行操作。下面来具体的说一下什么是 Collections类

一、Collections是什么

  Collections类是java.util下的一个类,属于工具类,提供了一系列的静态方法对集合进行各种操作,如果对该方法提供的对象为null时则会抛出“空指针异常”。Collections类提供的sort()方法便可以实现对集合中的元素进行排序的功能,当然前提是该对象所在的类是实现了Comparable接口的类型才可以,这就好比要对学生进行排序,那肯定得按照某种属性进行排序,年龄或者身高,有可比性的方可以进行排序。Collections提供的sort()方法中可以对Integer、String 类型的进行排序,因为Integer、String 等都是实现了Comparable接口的类型,Comparable接口便是这种规定,默认是按照自然顺序进行排序,如果一个类实现了Comparable接口,那么该类必须实现Comparable提供的compareTo(Object o)方法,如果两个对象进行比较,调用compareTo()方法,如果返回大于0则前者比后者大,小于零则表示前者比后者小,说到这里则不能忘了Comparable的兄弟接口Comparator接口,Comparator接口不同于Comparable的是Comparator是可以临时指定进行比较的规则,为那些没有自然顺序的对象collection进行排序,要实现Comparator接口就要实现compare方法,compare()方法中由两个对象,compare(Object obj1,Object,obj2)内部实现是obj1.compareTo(obj2)返回int的值,和compareTo返回值的意义相同。

二、Collections.sort()的实现

package CollectionDemo;

import java.util.*;

public class CollectionTest {

    //测试collection 的sort的方法Integer
    public void test1(){
        int k;
        List<Integer> integersList = new ArrayList<Integer>();
        Random  r = new Random();
        for(int i=0;i<10;i++){
            do {
                k = r.nextInt(100);
            }while(integersList.contains(k));
            integersList.add(k);
            System.out.println("成功添加数据:"+k);
        }
        System.out.println("----------排序前------------");
        for (Integer li:
             integersList) {
            System.out.println("元素:"+li);
        }
        Collections.sort(integersList);
        System.out.println("----------排序后----------");
        for (Integer li:
                integersList) {
            System.out.println("元素:"+li);
        }
    }
    //测试collection 的String 类型的
    public void test2(){
        List<String> stringList = new ArrayList<>();
        stringList.add("microsoft");
        stringList.add("google");
        stringList.add("alibaba");
        System.out.println("-----------排序前----------");
        for (String s:
             stringList) {
            System.out.println(s);
        }
        Collections.sort(stringList);
        System.out.println("------排序后----------");
        for (String d:
                stringList) {
            System.out.println(d);
        }
    }
  
    public static void main(String[] args){
        CollectionTest  ct = new CollectionTest();
               ct.test1();
               ct.test2();
      
     }
}

  上边的代码就是实现了Comparable的接口的类(如Integer、String)对象进行的排序

如果对学生类进行排序,便不能直接用上面的例子,因为学生的属性比较多,而没有默认的排序规则,只能通过指定他的排序规则,如按照年龄、姓名等属性进行排序。

package CollectionDemo;

import java.util.HashSet;
import java.util.Set;

/**
 * 学生类
 */
public class Student implements Comparable<Student>{
    private int id;
    private String name;
    public Set<Course> courses;


    public Student(int id,String name){
        this.id = id;
        this.name = name;
        this.courses  = new HashSet();
    }

    public void setId(int id){
        this.id = id;
    }


    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        return name != null ? name.hashCode() : 0;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Student o) {

        return this.getId()-o.getId();
    }
}

  

package CollectionDemo;

import java.util.Comparator;

public class StudentCompator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

  这段代码便是指定按照学生的id或者姓名进行排序。

以上只是大概的总结了一下Collections.sort()的方法以及Comparable接口和Comparator接口的实现。

原文地址:https://www.cnblogs.com/future-liu1121/p/6626520.html