常用工具类-Objects

常用工具类-Objects

  • ①判断对象是否为空
  • ②判断两个对象是否相等
  • ③比较两个对象
  • ④判断两个数组是否相等

Objects工具类位于java.util包中,提供了对对象操作的一些实用方法

常用工具类-Objects_第1张图片

①判断对象是否为空

  1. isNull() 判断对象是否为空
  2. noNull() 判断对象是否不为空
  3. requireNull()检查对象是否为null,如果为null则抛出NullPointerException异常,否则返回该对象。
Integer num1 =  10;
Integer num2 =  null;

// isNull() 判断对象是否为空
System.out.println(Objects.isNull(num1)); // false
System.out.println(Objects.isNull(num2)); // ture

// nonNull  判断对象是否不为空
System.out.println(Objects.nonNull(num1));
System.out.println(Objects.nonNull(num2));

// requireNull()
// 当对象为空时,抛出空指针异常 NullPointerException;
// 不为空时,则返回当前对象的值
System.out.println(Objects.requireNonNull(num1));

②判断两个对象是否相等

Objects.equals() ,可以在判断两个对象时规避掉空指针,但是当数据类型不同值相同时,会有一点坑。
使用Object.euqals()判断类的两个对象时,需要自己重写类的equals()方法

// 使用Objects.equals() 可以规避掉空指针,但是当数据类型但值相同时,也会判定为不相等false
Integer integer1 = 1;
Integer integer2 = 1;
Long integer3 = 1L;
Integer integer4 = null;
System.out.println(Objects.equals(integer1, integer2)); // true
System.out.println(Objects.equals(integer1, integer3)); // false
System.out.println(Objects.equals(integer1, integer4)); // false

// 两个对象的equals比较,需要自己重写 实体类的equals()方法
People p1 = new People("zs","男",18);
People p2 = new People("lisi","男",18);
People p3 = new People("lisi","男",18);
System.out.println(Objects.equals(p1, p2)); //  false
System.out.println(Objects.equals(p2, p3)); //  true

People类:

import java.util.Objects;

public class People {
    private String name;
    private String sex;

    private int age;


    public People() {
    }

    public People(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        People people = (People) o;
        return age == people.age && Objects.equals(name, people.name) && Objects.equals(sex, people.sex);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, sex, age);
    }
}



③比较两个对象

Objects.compare()方法用于比较两个对象,并且可以传入一个Comparator比较器接口,以用于自定义比较器规则。

People p5 = new People("chenfan","男",22);
People p6 = new People("zhujiu","男",19);
People p7 = new People("zhujiu","男",19);
// 比较器对象
Comparator<People> nameComparator = Comparator.comparing(p->p.getAge());

// left>right 返回1 ,left
System.out.println(Objects.compare(p5, p6, nameComparator));  // 1
System.out.println(Objects.compare(p6, p5, nameComparator));  // -1
System.out.println(Objects.compare(p6, p7, nameComparator));  // 0

④判断两个数组是否相等

Objects.deepEquals() 经常用于比较两个数组的内容是否相等

String[] strArr1 = new String[]{"a","b","c"};
String[] strArr2 = new String[]{"d","e","f"};
String[] strArr3 = new String[]{"d","e","f"};
System.out.println(Objects.deepEquals(strArr1, strArr2)); // false
System.out.println(Objects.deepEquals(strArr2, strArr3)); // true

你可能感兴趣的:(Java知识回顾,java,开发语言)