天天看点

java-集合类(3)-实现比较器(Comparator)接口-LinkedList针对插入删除、开始处增加元素

实现比较器(Comparator)接口

java-集合类(3)-实现比较器(Comparator)接口-LinkedList针对插入删除、开始处增加元素

实现比较器例子:

package ArrayList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

class ArrayListTest {

    public static void printElements(Collection<?> c) {
        Iterator<?> it = c.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    public static void main(String[] args) {
        Student s1 = new Student(, "xiaoxi");
        Student s2 = new Student(, "xiaohong");
        Student s3 = new Student(, "xiaozhu");
        ArrayList<Student> al = new ArrayList<Student>();
        al.add(s1);
        al.add(s2);
        al.add(s3);
        // Collections类进行排序
        Collections.sort(al, new Student.StudentComparator());
        printElements(al);
    }
}

class Student /*implements Comparable<Object> */{
    int num;
    String name;
    //实现比较器(Comparator)接口。声明为static 好处是不用产生一个外部类对象再产生一个内部类对象而是直接用外部类名称来产生一个内部类对象
    static class StudentComparator implements Comparator<Student> {

        @Override
        public int compare(Student o1, Student o2) {
            return o1.num > o2.num ?  : (o1.num == o2.num ?  : -);
        }
    }

    Student(int num, String name) {
        this.name = name;
        this.num = num;
    }

    /*@Override
    public int compareTo(Object arg0) {
        Student s = (Student) arg0;
        // 如果当前数比你要比较的数大返回1,小,返回负数
        return num > s.num ? 1 : (num == s.num ? 0 : -1);
    }*/
    //格式化输出格式
    public String toString() {
        return "num=" + num + ", name=" + name;
    }

}
           

不用上一节的继承Comparable方式

变式1:

package ArrayList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

class ArrayListTest {

    public static void printElements(Collection<?> c) {
        Iterator<?> it = c.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    public static void main(String[] args) {
        Student s1 = new Student(, "xiaoxi");
        Student s2 = new Student(, "xiaohong");
        Student s3 = new Student(, "xiaozhu");
        Student s4 = new Student(, "miaoli");
        ArrayList<Student> al = new ArrayList<Student>();
        al.add(s1);
        al.add(s2);
        al.add(s3);
        al.add(s4);
        // Collections类进行排序,第二个参数是自定义比较器
        Collections.sort(al, new Student.StudentComparator());
        printElements(al);
    }
}

class Student /*implements Comparable<Object> */{
    int num;
    String name;
    //实现比较器(Comparator)接口。声明为static 好处是不用产生一个外部类对象再产生一个内部类对象而是直接用外部类名称来产生一个内部类对象
    static class StudentComparator implements Comparator<Student> {

        @Override
        public int compare(Student o1, Student o2) {
            int result = o1.num > o2.num ?  : (o1.num == o2.num ?  : -);
            if(result == ){
                //当我们要比较的学号有一样的,那么就比较名字
                result = o1.name.compareTo(o2.name);
            }
            return result;
        }
    }

    Student(int num, String name) {
        this.name = name;
        this.num = num;
    }

    /*@Override
    public int compareTo(Object arg0) {
        Student s = (Student) arg0;
        // 如果当前数比你要比较的数大返回1,小,返回负数
        return num > s.num ? 1 : (num == s.num ? 0 : -1);
    }*/
    //格式化输出格式
    public String toString() {
        return "num=" + num + ", name=" + name;
    }

}
           

学号重复比较名字,修改比较器

结果:

num=, name=miaoli
num=, name=xiaohong
num=, name=xiaozhu
num=, name=xiaoxi
           

在已排序的List中搜索指定的元素:Collectons.binarySearch()。Collections 类主要针对列表操作,arrays类对数组操作

LinkedList

先引出数据结构:

ArrayList和LinkedList的比较

1、ArrayList底层采用数组完成,而LinkedList则是以一般的双向链表(double-linked list)完成,其内每个对象除了数据本身外,还有两个 引用,分别指向前一个元素和后一个元素。

2、如果我们经常在List的开始处增加元素,或者在List中进行插入和删除操作,我们应该使用LinkedList,否则的话,使用ArrayList将更加快速。

继续阅读