首页 技术 正文
技术 2022年11月16日
0 收藏 612 点赞 3,917 浏览 2789 个字

第一个例子

  1. /*为了比较,让自己的类实现Comparable接口,按照自己想要的排序方式重写compareTo
  2. *Map只是提供了对键的排序,但是当我们需要对值排序时就的提供我们自己的比较器 这里 只是模拟了Map但是实际上并没有使用Map
  3. */
  4. import java.util.Iterator;
  5. import java.util.Set;
  6. import java.util.TreeSet;
  7. public class SortByValue {
  8. public static void main(String[] args) {
  9. Set<Pair> set = new TreeSet<Pair>();
  10. set.add(new Pair(“me”, “1000”));
  11. set.add(new Pair(“and”, “4000”));
  12. set.add(new Pair(“you”, “3000”));
  13. set.add(new Pair(“food”, “10000”));
  14. set.add(new Pair(“hungry”, “5000”));
  15. set.add(new Pair(“later”, “6000”));
  16. set.add(new Pair(“myself”, “1000”));
  17. for (Iterator<Pair> i = set.iterator(); i.hasNext();)
  18. // 我喜欢这个for语句
  19. System.out.println(i.next());
  20. }
  21. }
  22. class Pair implements Comparable<Object> {
  23. private final String name;
  24. private final int number;
  25. public Pair(String name, int number) {
  26. this.name = name;
  27. this.number = number;
  28. }
  29. public Pair(String name, String number) throws NumberFormatException {
  30. this.name = name;
  31. this.number = Integer.parseInt(number);
  32. }
  33. public int compareTo(Object o) {
  34. if (o instanceof Pair) {
  35. // int cmp = Double.compare(number, ((Pair) o).number);
  36. int cmp = number – ((Pair) o).number;
  37. if (cmp != 0) {// number是第一要比较的,相当于先比较value。如果相同再比较键
  38. return cmp;
  39. }
  40. return name.compareTo(((Pair) o).name);
  41. }
  42. throw new ClassCastException(“Cannot compare Pair with “
  43. + o.getClass().getName());
  44. }
  45. public String toString() {
  46. return name + ‘ ‘ + number;
  47. }
  48. }
  49. 输出结果:
  50. me 1000
  51. myself 1000
  52. you 3000
  53. and 4000
  54. hungry 5000
  55. later 6000
  56. food 10000

第二个例子:

  1. import java.util.*;
  2. public class NameSort {
  3. public static void main(String[] args) {
  4. Name[] nameArray = { new Name(“John”, “Lennon”),
  5. new Name(“Karl”, “Marx”), new Name(“Groucho”, “Marx”),
  6. new Name(“Oscar”, “Grouch”) };
  7. Arrays.sort(nameArray);  //根据元素的自然顺序对指定对象数组按升序进行排序。数组中的所有元素都必须实现 Comparable 接口。此外,数组中的所有元                                      //素都必须是可相互比较的(也就是说,对于数组中的任何 e1e2 元素而言,e1.compareTo(e2) 不得抛出 ClassCastException)。
  8. for (int i = 0; i < nameArray.length; i++) {
  9. System.out.println(nameArray[i].toString());
  10. }
  11. }
  12. }
  13. class Name implements Comparable<Name> {
  14. public String firstName, lastName;
  15. public Name(String firstName, String lastName) {
  16. this.firstName = firstName;
  17. this.lastName = lastName;
  18. }
  19. public int compareTo(Name o) { // 实现接口
  20. int lastCmp = lastName.compareTo(o.lastName);
  21. // 首先比较姓(lastName)如果姓相同(lastCmp==0)再比较名(firstName),否则返回名的比较
  22. return (lastCmp == 0 ? firstName.compareTo(o.firstName) : lastCmp);
  23. }
  24. public String toString() { // 便于输出测试
  25. return firstName + ” ” + lastName;
  26. }
  27. }
  28. 输出结果:
  29. Oscar Grouch
  30. John Lennon
  31. Groucho Marx
  32. Karl Marx
    1. //看看这个三目运算符的漂亮应用哦!
    2. public int compareTo(Pair o) {
    3. int cmp = number – o.number;
    4. return (cmp == 0 ? name.compareTo(o.name) : cmp);
    5. }
    6. ———————-
    7. public int compareTo(Name o) { // 实现接口
    8. int lastCmp = lastName.compareTo(o.lastName);
    9. // 首先比较姓(lastName)如果姓相同(lastCmp==0)再比较名(firstName),否则返回名的比较
    10. return (lastCmp == 0 ? firstName.compareTo(o.firstName) : lastCmp);  
      1. }

        本文转载至:http://ocaicai.iteye.com/blog/794438

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,496
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,743
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298