Java 集合框架三

类Collections是一个包装类。它包含有各种有关集合操作的静态多态方法。此类不能实例化,就像一个工具类,服务于Java的Collection框架。

Comparator 和 Comparable 比较

Comparable是排序接口;若一个类实现了Comparable接口,就意味着“该类支持排序”。
而Comparator是比较器;我们若需要控制某个类的次序,可以建立一个“该类的比较器”来进行排序。

我们不难发现:Comparable相当于“内部比较器”,而Comparator相当于“外部比较器”。

用代码示例:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * 将要完成:
 * 1.通过Collections.sort()方法,对Integer泛型的List进行排序;
 * 2.对String泛型的List进行排序;
 * 3.对其他类型泛型的List进行排序,以Student为例。
 */
 public class CollectionsTest {

/**
 * 1.通过Collections.sort()方法,对Integer泛型的List进行排序;
 * 创建一个Integer泛型的List,插入十个100以内的不重复随机整数,
 * 调用Collections.sort()方法对其进行排序
 */
public void testSort1() {
    List<Integer> integerList = new ArrayList<Integer>();
    // 插入十个100以内的不重复随机整数
    Random random = new Random();
    Integer k;
    for (int i = 0; i < 10; i++) {
        do {
            k = random.nextInt(100);
        } while (integerList.contains(k));
        integerList.add(k);
        System.out.println("成功添加整数:" + k);
    }
    System.out.println("-------------排序前--------------");
    for (Integer integer : integerList) {
        System.out.println("元素:" + integer);
    }
    Collections.sort(integerList);
    System.out.println("----------------排序后-------------------");
    for (Integer integer : integerList) {
        System.out.println("元素:" + integer);
    }
}

/**
 * 2.对String泛型的List进行排序;
 * 创建String泛型的List,添加三个乱序的String元素,
 * 调用sort方法,再次输出排序后的顺序
 */
public void testSort2() {
    List<String> stringList = new ArrayList<String>();
    stringList.add("microsoft");
    stringList.add("google");
    stringList.add("lenovo");
    System.out.println("------------排序前-------------");
    for (String string : stringList) {
        System.out.println("元素:" + string);
    }
    Collections.sort(stringList);
    System.out.println("--------------排序后---------------");
    for (String string : stringList) {
        System.out.println("元素:" + string);
    }
}

/**
 * 3.对其他类型泛型的List进行排序,以Student为例。
 */
public void testSort3() {
    List<Student> studentList = new ArrayList<Student>();
    Random random = new Random();
    studentList.add(new Student(random.nextInt(1000) + "", "Mike"));
    studentList.add(new Student(random.nextInt(1000) + "", "Angela"));
    studentList.add(new Student(random.nextInt(1000) + "", "Lucy"));
    studentList.add(new Student(10000 + "", "Beyonce"));
    System.out.println("--------------排序前---------------");
    for (Student student : studentList) {
        System.out.println("学生:" + student.id + ":" + student.name);
    }
    Collections.sort(studentList);
    System.out.println("----------------排序后------------------");
    for (Student student : studentList) {
        System.out.println("学生:" + student.id + ":" + student.name);
    }
    Collections.sort(studentList, new StudentComparator());
    System.out.println("----------------按照姓名排序后-----------------");
    for (Student student : studentList) {
        System.out.println("学生:" + student.id + ":" + student.name);
    }
}

/**
 * @param args
 */
public static void main(String[] args) {
    CollectionsTest ct = new CollectionsTest();
    ct.testSort1();
    ct.testSort2();
    ct.testSort3();
}
}


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

/**
* 学生类
* @author Administrator
*
*/
public class Student implements Comparable<Student> {

public String id;

public String name;

public Set<Course> courses;

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

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof Student))
        return false;
    Student other = (Student) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

//@Override
public int compareTo(Student o) {
    // TODO Auto-generated method stub
    return this.id.compareTo(o.id);
}
}


import java.util.Comparator;

public class StudentComparator implements Comparator<Student> {

@Override
public int compare(Student o1, Student o2) {
    // TODO Auto-generated method stub
    return o1.name.compareTo(o2.name);
}

}

通过以上示例,就能明白Collections的用法,和Comparator的用法.

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值