zoukankan      html  css  js  c++  java
  • ht-8 对arrayList中的自定义对象排序( Collections.sort(List<T> list, Comparator<? super T> c))

     1 package com.iotek.set;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Collections;
     5 import java.util.Comparator;
     6 import java.util.List;
     7 /**
     8  * 
     9  * 对ArrayList容器中的内容进行排序: ArrayList中存储多个Person对象(具有name,age,id属性),
    10  * 要求按照年龄从小到大排序,年龄相等的话再按照名字的自然顺序来排序输出
    11  * 思路:
    12  * 使用ArrayList来存储Person对象,使用Collections类所提供的静态方法sort来按照要求对
    13  * ArrayList进行排序,然后输出排序好的信息。
    14  * @author Administrator
    15  *
    16  */
    17 public class CollectionsDemo2 {
    18     /* 1.创建一个ArrayList容器
    19      * 2.创建一个Person类,具有name,age,id属性
    20      * 3.对容器中的数据排序,用Collections类的方法sort对List接口的实现类排序
    21      * 4.输出排序好的内容 */
    22     
    23     public static void main(String[] args) {
    24         List<Personc> data = new ArrayList<Personc>();
    25         data.add(new Personc("jack",20,10));
    26         data.add(new Personc("rose",10,7));
    27         data.add(new Personc("mary",30,6));
    28         data.add(new Personc("zhang",50,18));
    29         data.add(new Personc("jay",20,11));
    30         Collections.sort(data, new Comparator<Personc>() {
    31             @Override
    32             public int compare(Personc o1, Personc o2) {
    33 //                首先按年龄来排序
    34                 if(o1.getAge() - o2.getAge() > 0) {
    35                     return 1;
    36                 } else if(o1.getAge() - o2.getAge() < 0) {
    37                     return -1;
    38                 } else { //年龄相等时,再按照名字来进行排序,
    39                     /*具体的字符串是String类的实例化对象,可以调用String类的
    40                      * compareTo(String anotherString)方法来对字符串按照字典顺序进行排序
    41 */                    return o1.getName().compareTo(o2.getName()); 
    42                 }
    43             }
    44         });
    45         
    46         for(Personc p : data) {
    47             System.out.println(p.toString());
    48         }
    49     }
    50 
    51 }
    52 /* sort(List<T> list, Comparator<? super T> c) 根据指定比较器产生的顺序对指定列表进行排序
    53  * Comparator<? super T> c   c表示一个比较器,比较器可以用匿名内部类来实现
    54  * 匿名内部类产生的是个接口的实现类对象,因此要实现这个接口中的compare()方法   */
    55 /*String.compareTo(String anotherString) 按字典顺序比较两个字符串,返回一个int类型的值*/
    56 
    57 class Personc {
    58     private String name;
    59     private int age;
    60     private int id;
    61     public Personc(String name, int age, int id) {
    62         super();
    63         this.name = name;
    64         this.age = age;
    65         this.id = id;
    66     }
    67     public String getName() {
    68         return name;
    69     }
    70     public void setName(String name) {
    71         this.name = name;
    72     }
    73     public int getAge() {
    74         return age;
    75     }
    76     public void setAge(int age) {
    77         this.age = age;
    78     }
    79     public int getId() {
    80         return id;
    81     }
    82     public void setId(int id) {
    83         this.id = id;
    84     }
    85     @Override
    86     //重写toString方法,定义打印格式
    87     public String toString() {
    88         return "Person [name=" + name + ", age=" + age + ", id=" + id + "]";
    89     }
    90     
    91 }
  • 相关阅读:
    Hadoop启动报Error: JAVA_HOME is not set and could not be found
    mrjob在hadoop上跑的时候,报错
    Hadoop3安装踩坑 there is no HDFS_NAMENODE_USER defined. Aborting operation.
    mrjob 运行报错
    站位

    Lua基本数据类型
    常量指针和指针常量
    C基础题
    C++拷贝构造函数(深拷贝,浅拷贝)
  • 原文地址:https://www.cnblogs.com/enjoyjava/p/9393398.html
Copyright © 2011-2022 走看看