zoukankan      html  css  js  c++  java
  • 比较

    比较器:
     Comparable: 内部比较器,实现相对简单 缺点是要修改源代码
     Comparator: 外部比较器,优点是不需要修改源代码 

    Comparable:  内部比较器

    package Content;
    
    import java.lang.reflect.Array;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.TreeSet;
    
    
    
    //Comparable:  内部比较器
    public class CollectionSortDemo_Comparable {
        
        static class Student implements Comparable<Student>{
            String name;
            int mark;
            
            public Student (String name,int mark) {
                this.name = name;
                this.mark = mark;
            }
            
            public int compareTo(Student oStudent) {
                //this>o 从小到大
                if(this.mark>oStudent.mark) {
                    return 1;
                }else if(this.mark<oStudent.mark) {
                    return -1;
                }else {
                    return this.name.compareTo(oStudent.name);
                }
            }
            
        }
        
        public static void main(String[] args) {
            
            ArrayList<String> arrayList = new ArrayList<String>();
            arrayList.add("Jhon");
            arrayList.add("Tom");
            arrayList.add("Jerry");
            arrayList.add("Anna");
            System.out.println(arrayList);
            
            //排序
            Collections.sort(arrayList);
            System.out.println(arrayList);
            System.out.println();
            
            ArrayList<Integer> arrayList2 = new ArrayList<Integer>();
            arrayList2.add(6);
            arrayList2.add(5);
            arrayList2.add(1);
            arrayList2.add(3);
            arrayList2.add(2);
            
            System.out.println(arrayList2);
            Collections.sort(arrayList2);
            System.out.println(arrayList2);
            System.out.println();
            
            ArrayList<Student> studentlist = new ArrayList<>();
            studentlist.add(new Student("Jhon", 80));
            studentlist.add(new Student("Tom", 60));
            studentlist.add(new Student("Jerry", 90));
            studentlist.add(new Student("Amy", 90));
            studentlist.add(new Student("Anna", 100));
            
            for(Student e:studentlist) {
                System.out.println(e.name+"  "+e.mark);
            }
            System.out.println();
            System.out.println("Sort:");
            Collections.sort(studentlist);
            for(Student e:studentlist) {
                System.out.println(e.name+"  "+e.mark);
            }
            
            System.out.println();
            
            System.out.println("TreeSet:");
            TreeSet<Student> studentSet = new TreeSet<>();
            studentSet.add(new Student("Jhon", 80));
            studentSet.add(new Student("Tom", 60));
            studentSet.add(new Student("Jerry", 90));
            studentSet.add(new Student("Amy", 90));
            studentSet.add(new Student("Anna", 100));
            
            for(Student e:studentSet) {
                System.out.println(e.name+" "+e.mark);
            }
            
            
        }
    
    }
    View Code

    Comparator:  外部比较器

    package Content;
    
    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.TreeSet;
    
    //Comparator:  外部比较器
    public class CollectionSortDemo_Comparator {
        
        static class Employee {
            String name;
            double salary;
            public Employee(String name,double salary) {
                this.name = name;
                this.salary = salary;
            }
        }
        static class EmployeeComparator implements Comparator<Employee>{
            public int compare(Employee oEmployee, Employee oEmployee2) {
                if (oEmployee.salary> oEmployee2.salary) {
                    return 1;
                } else if (oEmployee.salary < oEmployee2.salary) {
                    return -1;
                } else {
                    return oEmployee.name.compareTo(oEmployee2.name);
                }
            }
        }
        public static void main(String[] args) {
            TreeSet<Employee> employeesSet = new TreeSet<>(new EmployeeComparator());
            employeesSet.add(new Employee("Jhon", 8000));
            employeesSet.add(new Employee("Tom", 6000));
            employeesSet.add(new Employee("Jerry", 9000));
            employeesSet.add(new Employee("Anna", 10000));
            
            for(Employee e:employeesSet) {
                System.out.println(e.name+" "+e.salary);
            }
            
    //        Collections.sort(employeeslist,new EmployeeComparator());
    
        }
    
    }
    View Code

     

  • 相关阅读:
    机械设计手册
    如何在PADS的封装中做非金属化孔
    【Protle99SE】PCB中各层的含义【小汇】
    Stm32时钟分析
    头文件为什么要加#ifndef #define #endif
    android adb常用指令
    安装 SQLManagementStudio_x86_CHS(SQL Server Management Studio) 老提示重启的解决办法
    SQL 2008下载地址以及全新安装详细过程
    【原创】pads2007 Layout 电气连接性检查过孔显示错误
    十款最具发展前景机器人
  • 原文地址:https://www.cnblogs.com/Lemon1234/p/11619987.html
Copyright © 2011-2022 走看看