Java对象的比较

2023-11-09

在Java中的比较有两种:基本类型之间的比较和引用类型之间的比较

         对于基本类型来说,可以进行直接的比较:

int / long / short / byte,可以用 <、>、== 进行比较,返回值为 true 或者 false

char ,也是用 <、>、== 进行比较,根据Unicode码值比较大小,返回值为 true 或者 false

boolean ,只能用 == 和 != 进行比较,相同返回true,不同返回false

public static void main(String[] args) {
        //整型类型比较
        int a = 10;
        int b = 20;
        //返回 true 或者 false
        System.out.println(a > b);      //false
        System.out.println(a < b);      //true
        System.out.println(a == b);     //false

        //字符型比较
        char c = 'A';
        char d = 'B';
        //返回 true 或者 false
        System.out.println(c > d);      //false
        System.out.println(c < d);      //true
        System.out.println(c == d);     //false

        //布尔类型比较
        boolean e = true;
        boolean f = false;
        //只能用 == 或者 != 来比较
        System.out.println(e == f);     //false
        System.out.println(e != f);     //true

    }

        对于引用类型,必须用 == 或者 equals()进行比较:

引用类型的比较只有 == (!=)和equals,不存在< 、>、 <=、 >=的比较

== 是比较引用类型的同一性,当指向对象是同一个的时候,返回值为true,反之,返回false

equals是比较引用类型的相等性,当指向的对象“相等”时,返回值为true,反之,返回false(前提是,该类正确重写了equals)

        String s1 = new String("hello");
        String s2 = new String("hello");
        String s3 = new String("world");
        String s4 = s1;
        System.out.println(s1 == s2);       //false
        System.out.println(s1 == s3);       //false
        System.out.println(s1 == s4);       //true
        System.out.println(s1.equals(s2));      //true
        System.out.println(s1.equals(s3));      //false
        System.out.println(s1.equals(s4));      //true

关于比较两个对象的大小,有两个接口Comparable和Comparator

        Comparable和Comparator都是接口,Comparable中有一个方法compareTo(T o),Comparator中有一个方法 compare(T o1,T o2)。它们的不同在于,Comparable是一个具备比较能力的接口,但是Compartor是一个比较器,需要自己定义一个类来写比较方法。

        在Java中大部分类都实现了Comparable并且要比较的对象本身就有自然顺序,我们可以直接使用方法compareTo(T o),根据返回值来判断对象的大小,但是有些类并没有实现Comparable并且没有自然顺序,或者是我们想要的排序并不是按照自然顺序来排序的,这个时候我们就需要实现Comparator来进行比较。

        Comparable:是 this 和 o 的比较,当 this > o时,返回一个任意一个正数,当 this < o时,返回任意一个负数,当 this = o 时,返回0

        所有实现了 Comparable 接口的类,该类型的变量都可以使用 compareTo 方法,不论是自己写的还是 Java 自带的。

举例1:

//Person 类
public class Person implements Comparable<Person> {
    private final int height;
    private final int weight;
    private final int score;
    private final int level;
    @Override
    public int compareTo(Person o){
        if(this.level > o.level){
            return 1;
        }else if(this.level < o.level){
            return -1;
        }else{
            return 0;
        }
    }
    public Person(int height,int weight,int score,int level){
        this.height = height;
        this.weight = weight;
        this.score = score;
        this.level = level;
    }
    @Override
    public String toString(){
        return "Person{" + " height=" + height + " weight=" + weight + " score=" + score + " level" + level +"}";
    }
}


//返回为Person{ height=1 weight=2 score=3 level4} 大于 Person{ height=3 weight=6 score=1 level2}
public static void main(String[] args) {
        Person p1 = new Person(1,2,3,4);
        Person p2 = new Person(3,6,1,2);

        int tmp = p1.compareTo(p2);
        if(tmp > 0){
            System.out.println(p1 + " 大于 " + p2);
        }else if(tmp < 0){
            System.out.println(p1 + " 小于 " + p2);
        }else{
            System.out.println(p1 + " 等于 " + p2);
        }
    }

举例2: 

//String 也实现了 Comparable 接口,所以 String 类型的变量也可以用 compareTo 进行比较
        String s1 = "hello";
        String s2 = "world";

        int cmp = s1.compareTo(s2);
        if(cmp > 0){
            System.out.println(s1 + "大于" + s2);
        }else if(cmp < 0){
            System.out.println(s1 + "小于" + s2);
        }else{
            System.out.println(s1 + "等于" + s2);
        }

Comparator:是<T> o1和<T> o2 的比较,一切比较都由自己来定义,子类实现Comparator接口,可以重写compara方法,Comparator主要用来一些不满足自然顺序的比较。

举例:

//ListComparator 类
public class ListComparator implements Comparator<List<Integer>> {
    @Override
    public int compare(List<Integer> o1, List<Integer> o2) {
        return o1.size() - o2.size();
    }
//返回值为:list1 大于 list2
public static void main(String[] args) {
        //List类 不是 Comparator 的实现类,所以用 List 来举例
        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);

        List<Integer> list2 = new ArrayList<>();
        list2.add(4);
        list2.add(5);

        Comparator<List<Integer>> com = new ListComparator();
        int tmp = com.compare(list1,list2);
        if(tmp < 0){
            System.out.println("list1 小于 list2");
        }else if(tmp > 0){
            System.out.println("list1 大于 list2");
        }else{
            System.out.println("list1 等于 list2");
        }
    }

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java对象的比较 的相关文章

随机推荐