zoukankan      html  css  js  c++  java
  • 集合案例--对ArrayList容器中的内容进行排序

    package com.Set;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class ArrayListDemo4 {
        public static void main(String[] args) {
            List<Person> list = new ArrayList<Person>();
            list.add(new Person("jak", 20));
            list.add(new Person("roos", 10));
            list.add(new Person("marry", 30));
            list.add(new Person("jack", 20));
            Collections.sort(list, new Comparator<Person>() {
    
                @Override
                public int compare(Person o1, Person o2) {
                    if (o1.getAge() - o2.getAge() > 0) {
                        return 1;
                    } else if (o1.getAge() - o2.getAge() < 0) {
                        return -1;
                    }
                    return o1.getName().compareTo(o2.getName());
                }
    
            });
            for(Person p:list) {
                System.out.println(p.getAge()+p.getName());
            }
        }
    }
    
    class Person {
        private String name;
        private int age;
    
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
    }
  • 相关阅读:
    flex 自定义事件
    ssis 不停执行的方法
    动态修改大小的Panel用户控件
    ssis 写文件到数据库
    sqlserver CheckSum
    poj1423
    poj1860
    poj1862
    poj1426
    poj1234
  • 原文地址:https://www.cnblogs.com/tanlei-sxs/p/9991872.html
Copyright © 2011-2022 走看看