zoukankan      html  css  js  c++  java
  • java基础---->Comparable和Comparator的使用

      Comparable和Comparator都可以实现排序,今天我们就开始两种比较排序接口的学习。

    Comparable的使用

    一、Comparable的文档说明:

    Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.

     二、Comparable有一个方法需要实现:int compareTo(T o)

    Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. 
    参数:
           o - 要比较的对象。 
    返回:
            负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。 
    抛出:
            ClassCastException - 如果指定对象的类型不允许它与此对象进行比较。

    三、Comparable的实例代码如下:

    定义一个Personal类,实现Comparable类,比较对象是Personal类自己。

    package com.huhx.compare;
    
    public class Person implements Comparable<Person> {
        private String username;
        private int age;
    
        public Person(String username, int age) {
            this.username = username;
            this.age = age;
        }
        public String getUsername() {
            return username;
        }public int getAge() {
            return age;
        }
        @Override
        public String toString() {
            return "username: " + username + ", age: " + age;
        }
    
        // 先age从小到大排序,如果年龄一样,那么username字符升序排序
        @Override
        public int compareTo(Person person) {
            if (age > person.age) {
                return 1;
            } else if (age == person.age) {
                return username.compareTo(person.username);
            } else if (age < person.age) {
                return -1;
            }
            return 0;
        }
    }

     写一个测试类CompareTest.java

    package com.huhx.compare;
    
    import java.util.Arrays;
    
    public class CompareTest {
        public static void main(String[] args) {
            Person[] persons = new Person[4];
            persons[0] = new Person("huhx", 22);
            persons[1] = new Person("Linux", 23);
            persons[2] = new Person("Tomhu", 23);
            persons[3] = new Person("Tomhu", 24);
            Arrays.sort(persons);
            
            for(Person person: persons) {
                System.out.println(person);
            }
        }
    }

    得到运行结果如下:

    username: huhx, age: 22
    username: Linux, age: 23
    username: Tomhu, age: 23
    username: Tomhu, age: 24

    Comparator的使用

    一、Comparator的文档说明:

    A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering.

    二、Comparator有一个方法需要实现:int compare(T o1, T o2)

    Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second

    三、Comparator的实例代码如下

    定义一个比较的对象Person类:

    package com.huhx.compator;
    
    public class Person {
        private String username;
        private int age;
    
        public Person(String username, int age) {
            this.username = username;
            this.age = age;
        }
    
        public String getUsername() {
            return username;
        }public int getAge() {
            return age;
        }
    
        @Override
        public String toString() {
            return "username: " + username + ", age: " + age;
        }
    }

    定义一个以Person为比较对象的Comparator:

    package com.huhx.compator;
    
    import java.util.Comparator;
    
    public class AgeComparator implements Comparator<Person>{
        // 先age从小到大排序,如果年龄一样,那么username字符降序排序
        @Override
        public int compare(Person person1, Person person2) {
            int compare = person1.getAge() - person2.getAge();
            if (compare == 0) {
                return person2.getUsername().compareTo(person1.getUsername());
            }
            return compare;
        }
    }

    写一个测试类CompatorTest.java

    package com.huhx.compator;
    import java.util.TreeSet;
    
    public class CompatorTest {
        public static void main(String[] args) {
            AgeComparator comparator = new AgeComparator();
            TreeSet<Person> set = new TreeSet<Person>(comparator);
            set.add(new Person("huhx", 22));
            set.add(new Person("Linux", 23));
            set.add(new Person("Tomhu", 23));
            set.add(new Person("Tomhu", 24));
    
            for (Person person : set) {
                System.out.println(person);
            }
        }
    }

    得到运行结果如下:

    username: huhx, age: 22
    username: Tomhu, age: 23
    username: Linux, age: 23
    username: Tomhu, age: 24

    友情链接

  • 相关阅读:
    Lazarus中system.length说明
    Lazarus中TScreen类使用介绍
    Delphi深度探索-CodeSite应用指南
    Lazarus如何变成XE的界面
    Lazarus IDE的几个小技术
    Delphi结构体数组指针的问题
    delphi动态数组指针问题
    delphi 数组类型与数组指针的巧妙利用
    Delphi Form的释放和隐藏:free,hide,close
    Delphi 记录类型- 结构指针
  • 原文地址:https://www.cnblogs.com/huhx/p/comparableAndcomparator.html
Copyright © 2011-2022 走看看