개발이야기/Java

[Java] ObjectUtils 클래스 메서드 사용법, 예제 (isEmpty, nullSafeEquals, isArray)

후린개발자 2024. 1. 18.
반응형

ObjectUtils 클래스는 Spring Framework에서 제공하는 유틸리티 클래스로서, 객체와 배열에 관련된 다양한 작업을 수행할 수 있습니다. ObjectUtils 클래스에서 자주 사용하는 ObjectUtils.isEmpty
, ObjectUtils.nullSafeEquals, ObjectUtils.isArray 메서드에 대해 작성하였습니다.

 

 

ObjectUtils.isEmpty

boolean isEmpty = ObjectUtils.isEmpty(myObject);

 

-객체가 비어있는지 여부를 확인하는 메서드로, 주로 문자열, 배열, 컬렉션 등을 확인할 때 사용됩니다. 객체가 null이면 true를 반환하고, 객체의 유형에 따라 비어있는지 여부를 확인합니다.

 

 

ObjectUtils.nullSafeEquals

boolean isEqual = ObjectUtils.nullSafeEquals(obj1, obj2);

 

-동등성 비교를 수행하는 메서드입니다. 두 객체가 모두 null이면 true를 반환하고, 그렇지 않으면 Objects.equals(o1, o2)와 동일하게 동작합니다.

-두 객체 중 하나가 null인 경우에도 NullPointerException을 발생시키지 않습니다. 둘 다 null이면 true를 반환하고, 그렇지 않으면 객체의 equals 메서드를 호출하여 동등성을 판단합니다.

 

 

ObjectUtils.isArray

boolean isArray = ObjectUtils.isArray(myObject);

 

-객체가 배열인지 여부를 확인하는 메서드입니다. 객체가 배열이면 true를 반환하고, 그렇지 않으면 false를 반환합니다.

 

 

소스코드

import java.util.Objects;
import org.springframework.util.ObjectUtils;

public class TEST {

    public static void main(String[] args) throws Exception {
        // 예제 1: Objects.equals, ObjectUtils.nullSafeEquals - 두 객체가 동등한지 확인
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = "World";
        
        Integer num1 = 42;
        Integer num2 = 42;
        Integer num3 = 99;

        boolean isEqual1 = Objects.equals(str1, str2);
        boolean isEqual2 = Objects.equals(str1, str3);

        boolean isEqual3 = ObjectUtils.nullSafeEquals(num1, num2);
        boolean isEqual4 = ObjectUtils.nullSafeEquals(num1, num3);

        System.out.println("예제 1 결과 1: " + isEqual1);  // 출력: true
        System.out.println("예제 1 결과 2: " + isEqual2);  // 출력: false
        System.out.println("예제 1 결과 3: " + isEqual3);  // 출력: true
        System.out.println("예제 1 결과 4: " + isEqual4);  // 출력: false
        
        System.out.println("--------------------");  

        // 예제 2: ObjectUtils.isEmpty - 객체가 비어있는지 확인 (null도 포함)
        String str4 = null;
        String str5 = "";
        String str6 = "Not Empty";
        
        Integer int1 = null;
        Integer int2 = 0;
        Integer int3 = 42;

        boolean isEmpty1 = ObjectUtils.isEmpty(str4);
        boolean isEmpty2 = ObjectUtils.isEmpty(str5);
        boolean isEmpty3 = ObjectUtils.isEmpty(str6);
        
        boolean isEmpty4 = ObjectUtils.isEmpty(int1);
        boolean isEmpty5 = ObjectUtils.isEmpty(int2);
        boolean isEmpty6 = ObjectUtils.isEmpty(int3);

        System.out.println("예제 2 결과 1: " + isEmpty1);  // 출력: true
        System.out.println("예제 2 결과 2: " + isEmpty2);  // 출력: true
        System.out.println("예제 2 결과 3: " + isEmpty3);  // 출력: false
        
        System.out.println("예제 2 결과 4: " + isEmpty4);  // 출력: true
        System.out.println("예제 2 결과 5: " + isEmpty5);  // 출력: false
        System.out.println("예제 2 결과 6: " + isEmpty6);  // 출력: false
        
        System.out.println("--------------------"); 
        
        // 예제 3: ObjectUtils.isArray - 배열인지 확인
        String[] array1 = null;
        String[] array2 = {};
        String[] array3 = {"Element1", "Element2"};
        Integer[] array4 = {1, 2, 3};
        
        Object nonArrayObject = "Not an array";

        boolean isArray1 = ObjectUtils.isArray(array1);
        boolean isArray2 = ObjectUtils.isArray(array2);
        boolean isArray3 = ObjectUtils.isArray(array3);
        boolean isArray4 = ObjectUtils.isArray(array4);
        boolean isArrayNonArrayObject = ObjectUtils.isArray(nonArrayObject);

        System.out.println("예제 3 결과 1: " + isArray1);  // 출력: false
        System.out.println("예제 3 결과 2: " + isArray2);  // 출력: true
        System.out.println("예제 3 결과 3: " + isArray3);  // 출력: true
        System.out.println("예제 3 결과 4: " + isArray4);  // 출력: true
        System.out.println("예제 3 결과 5: " + isArrayNonArrayObject);  // 출력: false
    }
}

 


console 화면

반응형

댓글

💲 추천 글