• java 中对象比较大小


    java 中对象比较大小 

    java 中对象比较大小有两种方法 
    1:实现Comparable 接口 的 public int compareTo(T o) 方法; 

    2:实现Comparator 接口 的   int compare(T o1, T o2)方法; 

    代码如下: 

    import java.util.ArrayList;  
    import java.util.Collections;  
    import java.util.Comparator;  
    import java.util.List;  
    import java.util.Set;  
    import java.util.TreeSet;  
      
    public class Test {  
        public static void main(String[] args) {  
            List list =new ArrayList();  
            list.add(new Dept("2","研发部",23));  
            list.add(new Dept("2","总公司",575));  
            list.add(new Dept("2","总公司",565));  
    //      Collections.sort(list);  
            Collections.sort(list, new Comparator(){  
      
                public int compare(Object arg0, Object arg1) {  
                    // TODO Auto-generated method stub  
                    Dept dept1=(Dept)arg0;  
                    Dept dept2=(Dept)arg1;  
                    return dept1.compareTo(dept2);  
                }  
                  
            });  
              
            for (int i=0;i<list.size();i++){  
                System.out.println((Dept)list.get(i));  
            }  
              
              
            Dept dept1=new Dept("2","研发部",23);  
            Dept dept2=new Dept("2","kk",44);  
            Set set=new TreeSet(new Comparator(){  
      
                public int compare(Object arg0, Object arg1) {  
                    // TODO Auto-generated method stub  
                    return 0;  
                }  
                  
            });  
            set.add(dept1);  
            set.add(dept2);  
              
          
        }  
    }  
      
    class Dept implements Comparable{  
        private String id;  
        private String name;  
        private long num;  
        public Dept(String id,String name,long num){  
            this.id=id;  
            this.name=name;  
            this.num=num;  
        }  
        public String getId() {  
            return id;  
        }  
      
        public void setId(String id) {  
            this.id = id;  
        }  
      
        public String getName() {  
            return name;  
        }  
      
        public void setName(String name) {  
            this.name = name;  
        }  
      
        public long getNum() {  
            return num;  
        }  
      
        public void setNum(long num) {  
            this.num = num;  
        }  
      
        public int compareTo(Object arg0) {  
            Dept dept=(Dept) arg0;  
            int i=0;  
            i=this.id.compareTo(dept.id);  
            if(i!=0){//部门id不相等  
                return i;  
            }else{//部门id相等  
                i=this.name.compareTo(dept.name);  
                if(i!=0){//部门名称不相等  
                    return i;  
                }else{//部门名称不相等  
                    if(this.num>dept.num)return 1;  
                    else if(this.num<dept.num)return -1;  
                    else return 0;  
                }  
            }  
              
        }  
        public String toString() {  
            // TODO Auto-generated method stub  
            return this.id+"-->"+this.name+"-->"+this.num;  
        }  
      
    }
  • 相关阅读:
    GitHub加速 ,GitHub下载速度太慢,国内github访问加速,github速度慢解决办法
    vscode 配置(格式化代码)
    微信小程序生成二维码(完美解决因链接过长报错问题)
    git 本地和仓库同步
    【转】JavaScript 判断iPhone X Series机型的方法
    【转】Js apply方法详解
    【转】JS生成指定范围内的随机数(支持随机小数)
    移动端浮动靠边按钮
    判断访问设备类型(android、ios、微信、pc)
    函数返回值
  • 原文地址:https://www.cnblogs.com/xu-xiao/p/6019253.html
Copyright © 2020-2023  润新知