• List<Obj>按obj排序


    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class Testa {
    
        public static void main(String[] args) {
            
            List<Obj> l = new ArrayList<>();
            Obj obj1 = new Obj(1,"一");
            Obj obj2 = new Obj(2,"二");
            Obj obj3 = new Obj(3,"三");
            l.add(obj1);
            l.add(obj3);
            l.add(obj2);
            
            
            Collections.sort(l, new Comparator<Obj>() {
    
                public int compare(Obj o1, Obj o2) {
    
                    // 升序
                    if (o1.getA() > o2.getA()) {
    
                        return 1;
                    }
                    if (o1.getA() == o2.getA()) {
    
                        return 0;
                    }
                    return -1;
                }
            });
            
            for (int i = 0; i < l.size(); i++) {
                
                System.out.println(l.get(i).toString());
            }
        }
    
    }
    
    class Obj {
    
        int a;
    
        String s;
        
        public Obj() {}
    
        public Obj(int a, String s) {
            super();
            this.a = a;
            this.s = s;
        }
    
        @Override
        public String toString() {
            return "Obj [a=" + a + ", s=" + s + "]";
        }
    
        public int getA() {
            return a;
        }
    
        public void setA(int a) {
            this.a = a;
        }
    
        public String getS() {
            return s;
        }
    
        public void setS(String s) {
            this.s = s;
        }
    
    }
    这个博客主要是javaEE相关或者不相关的记录, hadoop与spark的相关文章我写在下面地址的博客啦~ http://www.cnblogs.com/sorco
  • 相关阅读:
    java基础
    mysql入门
    基础整理
    遍历列表的两种方式
    oracle常用操作
    DIV+CSS网页布局技巧实例1:设置网页整体居中的代码
    html+css 淘宝首页静态页面案例
    WEB前端开发规范文档+CSS命名规范
    day05-苏宁易购导航html
    day04-html
  • 原文地址:https://www.cnblogs.com/orco/p/6524221.html
Copyright © 2020-2023  润新知