zoukankan      html  css  js  c++  java
  • java中的compareto方法以及LIst列表排序的详细介绍【转】

    java中的compareto方法,返回参与比较的前后两个字符串的asc码的差值,看下面一组代码
    String a="a",b="b";
    System.out.println(a.compareto.b);
    则输出-1;
    若a="a",b="a"则输出0;
    若a="b",b="a"则输出1;
     
    单个字符这样比较,若字符串比较长呢??
    若a="ab",b="b",则输出-1;
    若a="abcdef",b="b"则输出-1;
    也就是说,如果两个字符串首字母不同,则该方法返回首字母的asc码的差值;
     
    如果首字母相同呢??
    若a="ab",b="a",输出1;
    若a="abcdef",b="a"输出5;
    若a="abcdef",b="abc"输出3;
    若a="abcdef",b="ace"输出-1;
    即参与比较的两个字符串如果首字符相同,则比较下一个字符,直到有不同的为止,返回该不同的字符的asc码差值,如果两个字符串不一样长,可以参与比较的字符又完全一样,则返回两个字符串的长度差值

    java中的list 中sort排序结合compareTo方法的详细介绍

     1 /*测试类*/
     2 package test;
     3 import java.util.ArrayList;
     4 import java.util.Collections;
     5 import java.util.Comparator;
     6 import java.util.GregorianCalendar;
     7 import java.util.Iterator;
     8 import java.util.List;
     9 
    10 public class UseComparator {
    11     public static void main(String args[]) {  
    12         List<Book> list = new ArrayList<Book>(); // 数组序列  
    13         Book b1 = new Book(10000, "红楼梦", 150.86, new GregorianCalendar(2009,  
    14                 01, 25), "曹雪芹、高鄂");  
    15         Book b2 = new Book(10001, "三国演义", 99.68, new GregorianCalendar(2008, 7,  
    16                 8), "罗贯中 ");  
    17         Book b3 = new Book(10002, "水浒传", 100.8, new GregorianCalendar(2009, 6,  
    18                 28), "施耐庵 ");  
    19         Book b4 = new Book(10003, "西游记", 120.8, new GregorianCalendar(2011, 6,  
    20                 8), "吴承恩");  
    21         Book b5 = new Book(10004, "天龙八部", 10.4, new GregorianCalendar(2011, 9,  
    22                 23), "搜狐");  
    23         list.add(b1);  
    24         list.add(b2);  
    25         list.add(b3);  
    26         list.add(b4);  
    27         list.add(b5);  
    28         // Collections.sort(list); //没有默认比较器,不能排序  
    29         System.out.println("数组序列中的元素:");  
    30         myprint(list);  
    31         Collections.sort(list, new PriceComparator()); // 根据价格排序  
    32         System.out.println("按书的价格排序:");  
    33         myprint(list);  
    34         Collections.sort(list, new CalendarComparator()); // 根据时间排序  
    35         System.out.println("按书的出版时间排序:");  
    36         myprint(list);  
    37     }  
    38   
    39     // 自定义方法:分行打印输出list中的元素  
    40     public static void myprint(List<Book> list) {  
    41         Iterator<Book> it = list.iterator(); // 得到迭代器,用于遍历list中的所有元素  
    42         while (it.hasNext()) {// 如果迭代器中有元素,则返回true  
    43             System.out.println("	" + it.next());// 显示该元素  
    44         }  
    45     }  
    46   
    47     // 自定义比较器:按书的价格排序  
    48     static class PriceComparator implements Comparator<Object> {  
    49         public int compare(Object object1, Object object2) {// 实现接口中的方法  
    50             Book p1 = (Book) object1; // 强制转换  
    51             Book p2 = (Book) object2;  
    52             return new Double(p1.price).compareTo(new Double(p2.price));  
    53         }  
    54     }  
    55   
    56     // 自定义比较器:按书出版时间来排序  
    57     static class CalendarComparator implements Comparator<Object> {  
    58         public int compare(Object object1, Object object2) {// 实现接口中的方法  
    59             Book p1 = (Book) object1; // 强制转换  
    60             Book p2 = (Book) object2;  
    61             return p2.calendar.compareTo(p1.calendar);  
    62         }  
    63     }  
    64 }  
    结果如下:

    数组序列中的元素:
    10000 红楼梦 150.86 曹雪芹、高鄂 2009年02月25日
    10001 三国演义 99.68 罗贯中 2008年08月08日
    10002 水浒传 100.80 施耐庵 2009年07月28日
    10003 西游记 120.80 吴承恩 2011年07月08日
    10004 天龙八部 10.40 搜狐 2011年10月23日


    按书的价格排序:
    10004 天龙八部 10.40 搜狐 2011年10月23日
    10001 三国演义 99.68 罗贯中 2008年08月08日
    10002 水浒传 100.80 施耐庵 2009年07月28日
    10003 西游记 120.80 吴承恩 2011年07月08日
    10000 红楼梦 150.86 曹雪芹、高鄂 2009年02月25日


    按书的出版时间排序:
    10004 天龙八部 10.40 搜狐 2011年10月23日
    10003 西游记 120.80 吴承恩 2011年07月08日
    10002 水浒传 100.80 施耐庵 2009年07月28日
    10000 红楼梦 150.86 曹雪芹、高鄂 2009年02月25日
    10001 三国演义 99.68 罗贯中 2008年08月08日

     1 package test;
     2 import java.text.DecimalFormat;  
     3 import java.text.SimpleDateFormat;  
     4 import java.util.GregorianCalendar;  
     5 import java.util.Iterator;  
     6 import java.util.TreeMap;  
     7       
     8     /** 
     9      * 书实体类 
    10      *  
    11      * @author yjd 
    12      *  
    13      */  
    14     public class Book implements Comparable<Object> { // 定义名为Book的类,默认继承自Object类  
    15         public int id;// 编号  
    16         public String name;// 名称  
    17         public double price; // 价格  
    18         private String author;// 作者  
    19         public GregorianCalendar calendar;// 出版日期  
    20       
    21         public Book() {  
    22             this(0, "X", 0.0, new GregorianCalendar(), "");  
    23         }  
    24       
    25         public Book(int id, String name, double price, GregorianCalendar calender,  
    26                 String author) {  
    27             this.id = id;  
    28             this.name = name;  
    29             this.price = price;  
    30             this.calendar = calender;  
    31             this.author = author;  
    32         }  
    33       
    34         // 重写继承自父类Object的方法,满足Book类信息描述的要求  
    35         public String toString() {  
    36             String showStr = id + "	" + name; // 定义显示类信息的字符串  
    37             DecimalFormat formatPrice = new DecimalFormat("0.00");// 格式化价格到小数点后两位  
    38             showStr += "	" + formatPrice.format(price);// 格式化价格  
    39             showStr += "	" + author;  
    40             SimpleDateFormat formatDate = new SimpleDateFormat("yyyy年MM月dd日");  
    41             showStr += "	" + formatDate.format(calendar.getTime()); // 格式化时间  
    42             return showStr; // 返回类信息字符串  
    43         }  
    44       
    45         public int compareTo(Object obj) {// Comparable接口中的方法  
    46             Book b = (Book) obj;  
    47             return this.id - b.id; // 按书的id比较大小,用于默认排序  
    48         }  
    49       
    50         public static void main(String[] args) {  
    51             Book b1 = new Book(10000, "红楼梦", 150.86, new GregorianCalendar(2009,  
    52                     01, 25), "曹雪芹、高鄂");  
    53             Book b2 = new Book(10001, "三国演义", 99.68, new GregorianCalendar(2008, 7,  
    54                     8), "罗贯中 ");  
    55             Book b3 = new Book(10002, "水浒传", 100.8, new GregorianCalendar(2009, 6,  
    56                     28), "施耐庵 ");  
    57             Book b4 = new Book(10003, "西游记", 120.8, new GregorianCalendar(2011, 6,  
    58                     8), "吴承恩");  
    59             Book b5 = new Book(10004, "天龙八部", 10.4, new GregorianCalendar(2011, 9,  
    60                     23), "搜狐");  
    61             TreeMap tm = new TreeMap();  
    62             tm.put(b1, new Integer(255));  
    63             tm.put(b2, new Integer(122));  
    64             tm.put(b3, new Integer(688));  
    65             tm.put(b4, new Integer(453));  
    66             tm.put(b5, new Integer(40));  
    67             Iterator it = tm.keySet().iterator();  
    68             Object key = null, value = null;  
    69             Book bb = null;  
    70             while (it.hasNext()) {  
    71                 key = it.next();  
    72                 bb = (Book) key;  
    73                 value = tm.get(key);  
    74                 System.out.println(bb.toString() + "	库存:" + tm.get(key));  
    75             }  
    76         }  
    77     }  

    book类测试结果:

    10000   红楼梦   150.86   曹雪芹、高鄂   2009年02月25日  库存:255
    10001   三国演义   99.68   罗贯中    2008年08月08日     库存:122
    10002   水浒传   100.80   施耐庵    2009年07月28日    库存:688
    10003   西游记   120.80   吴承恩   2011年07月08日     库存:453
    10004   天龙八部   10.40   搜狐   2011年10月23日     库存:40

  • 相关阅读:
    初学c课程笔记整理7-->>二维数组、字符串、指针
    初学c课程笔记整理6-->>数组
    ipad上自定义view的旋转适配
    苹果mac电脑中brew的安装使用及卸载详细教程
    Apple、Google、Microsoft的用户体验设计原则
    iOS图片拉伸技巧
    子网掩码解析【转载】
    Ios8,Xcode6下 设置Launch Image 启动图片
    ios8 设置单元格分割线无效
    简述UIScrollView的属性和用法
  • 原文地址:https://www.cnblogs.com/Lxiaojiang/p/6805151.html
Copyright © 2011-2022 走看看