zoukankan      html  css  js  c++  java
  • Java中Comparable和Comparator实现对象比较

    1.通过Comparable实现排序

    package Comparable;
    
    import java.util.Arrays;
    
    public class ComparableUser implements Comparable<ComparableUser> 
    {
    
        private String id;
        private int age;
    
        public ComparableUser(String id, int age) {
            this.id = id;
            this.age = age;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public int compareTo(ComparableUser o) {
             return this.age - o.getAge();
             //return ((ComparableUser) o).getAge() - this.age;
        }
    
        /**
         * 测试方法
         */
        public static void main(String[] args) {
            ComparableUser[] users = new ComparableUser[] {
                    new ComparableUser("u1001", 25),
                    new ComparableUser("u1002", 20),
                    new ComparableUser("u1003", 21) };
            Arrays.sort(users);// 对对象进行排序
            for (int i = 0; i < users.length; i++) {
                ComparableUser user = users[i];
                System.out.println(user.getId() + " " + user.getAge());
            }
        }
    
    }

    2.通过实现Comparator进行排序

    package Comparable;
    
    import java.util.Arrays;
    import java.util.Comparator;
    
    class User {  
        
        private String id;  
        private int age;  
      
        public User(String id, int age) {  
            this.id = id;  
            this.age = age;  
        }  
      
        public int getAge() {  
            return age;  
        }  
      
        public void setAge(int age) {  
            this.age = age;  
        }  
      
        public String getId() {  
            return id;  
        }  
      
        public void setId(String id) {  
            this.id = id;  
        }  
    }
    
    public class UserComparator implements Comparator {  
          
        public int compare(Object arg0, Object arg1) {  
            return ((User) arg0).getAge() - ((User) arg1).getAge();  
        }  
      
        /** 
         * 测试方法 
         */  
        public static void main(String[] args) {  
            User[] users = new User[] { new User("u1001", 25),  
                    new User("u1002", 20), new User("u1003", 21) };  
            Arrays.sort(users, new UserComparator());  
            for (int i = 0; i < users.length; i++) {  
                User user = users[i];  
                System.out.println(user.getId() + " " + user.getAge());  
            }  
        }
    
    }  

    3.

    package Comparable;
    
    import java.util.Arrays;
    import java.util.Comparator;
    
    class User {
    
        private String id;
        private int age;
        private int score;
    
        public User(String id, int age, int score) {
            this.id = id;
            this.age = age;
            this.setScore(score);
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public int getScore() {
            return score;
        }
    
        public void setScore(int score) {
            this.score = score;
        }
    }
    
    public class UserComparator implements Comparator<User> {
    
        public int compare(User arg0, User arg1) {
            if(arg0.getAge()<arg1.getAge())
                return 1;
            else if(arg0.getAge()>arg1.getAge())
                return -1;
            else if(arg0.getScore()<arg1.getScore())
                return -1;
            else if(arg0.getScore()>arg1.getScore())
                return 1;
            return 0;
        }
    
        /**
         * 测试方法
         */
        public static void main(String[] args) {
            User[] users = new User[] { 
                    new User("u1001", 25, 90), new User("u1002", 20, 100), new User("u1003", 25, 91) };
            Arrays.sort(users, new UserComparator());
            for (int i = 0; i < users.length; i++) {
                User user = users[i];
                System.out.println(user.getId() + " " + user.getAge() + " " + user.getScore());
            }
        }
    
    }

     4.推荐使用案例:

    package bwzq.domain.player;
    
    import java.util.Date;
    
    public class PlayerNotice {
    
        private long id;private int priority;
        private int priority2;public PlayerNotice() {
        }
    
        public PlayerNotice(long id, int priority, int priority2) {
            this.id = id;
            this.priority = priority;
            this.priority2 = priority2;
        }
    
        public long getId() {
            return id;
        }
    
        public void setId(long id) {
            this.id = id;
        }public int getPriority() {
            return priority;
        }
    
        public void setPriority(int priority) {
            this.priority = priority;
        }public int getPriority2() {
            return priority2;
        }
    
        public void setPriority2(int priority2) {
            this.priority2 = priority2;
        }
    
    }
    package bwzq.domain.player;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class PlayerNoticeComparator implements Comparator<PlayerNotice> {
        @Override
        public int compare(PlayerNotice arg0, PlayerNotice arg1) {
            if (arg0.getPriority() < arg1.getPriority())
                return -1;
            else if (arg0.getPriority() > arg1.getPriority())
                return 1;
            else if (arg0.getPriority2() < arg1.getPriority2())
                return -1;
            else if (arg0.getPriority2() > arg1.getPriority2())
                return 1;
            return 0;
        }
    
        public static void main(String[] args) {
    
            List<PlayerNotice> list = new ArrayList<PlayerNotice>();
            list.add(new PlayerNotice(1, 1, 2));
            list.add(new PlayerNotice(2, 2, 1));
            list.add(new PlayerNotice(1, 2, 1));
            list.add(new PlayerNotice(11, 2, 3));
            list.add(new PlayerNotice(3, 2, 4));
            list.add(new PlayerNotice(7, 2, 5));
            list.add(new PlayerNotice(3, 4, 5));
            list.add(new PlayerNotice(5, 3, 8));
            list.add(new PlayerNotice(4, 5, 6));
    
            Collections.sort(list, new PlayerNoticeComparator());
            for (int i = 0; i < list.size(); i++) {
                System.out.println("Priority=" + list.get(i).getPriority() + " Priority2=" + list.get(i).getPriority2() + " Id=" + list.get(i).getId());
            }
        }
    }
  • 相关阅读:
    mysql5.7-Group Replication
    yum安装mariadb-galera同步
    开启tomcat的apr模式,并利用redis做tomcat7的session的共享。
    利用Xtrabackup在不停机的情况下备用数据库迁移
    open-falcon(v0.2)安装grafana部署
    open-falcon(v0.2)部署手册(源码编译)
    阿里云ECS主机自定义进程监控
    Open-Falcon第七步安装报警模块(小米开源互联网企业级监控系统)
    Open-Falcon第六步安装Dashboard(小米开源互联网企业级监控系统)
    Open-Falcon第五步安装Query(小米开源互联网企业级监控系统)
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3788076.html
Copyright © 2011-2022 走看看