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

     

  • 相关阅读:
    IE9 bug: 在textarea中复制内容会丢失换行符
    [IIS]修改MaxFieldLength与MaxRequestBytes彻底解决Request Too Long的问题
    百年一遇的奇怪问题:当IE遇上.NET Framework 4.5
    Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include
    cnzz统计代码引起的Bad Request Request Too Long
    看我72变:解决Entity Framework中枚举类型与tinyint的映射问题
    续篇:新型Lamda版Html.RenderAction
    System.Threading.Tasks.Task引起的IIS应用程序池崩溃
    ASP.NET/C# WebRequest POST Google OAuth API
    ASP.NET MVC中加载WebForms用户控件(.ascx)
  • 原文地址:https://www.cnblogs.com/Lemon1234/p/11619987.html
Copyright © 2011-2022 走看看