Java program to compare two arrays equal or not in size

In this tutorial, you will learn how to write Java program to compare two arrays and check that they are equal in size or not.

In Java, comparing two arrays to see if they are equal in size is a common task that helps in various programming scenarios, such as validating data before processing. This involves checking whether the number of elements in both arrays is the same. Java provides multiple ways to achieve this, each suitable for different situations or preferences. Understanding how to effectively compare the sizes of two arrays is an important skill for anyone learning Java, especially for tasks involving data manipulation and validation.

Below are the approach which we will be follow to achieve our goal:

  • Our approach is to check two given arrays are equal in size or not is first we will find the size of both array.
  • And then compare the size. If the size are equal then it will print “size of both arrays are equal” and if the size is not equal then it will print “size of arrays are not equal”.

How our program will behave?

As we have already seen above our logic to check the size of given two arrays is equal or not.

In this program we have already two arrays. After the execution of the program it will print the output as per array size after the calculation.

Java Program to check two arrays are equal or not in size

Program 1: Using Array Length Property

public class Main {  
    public static void main(String[] args) {
        int sum = 0;
        int arr1[]={1,2,3,4,5};
        int arr2[]={2,3,1,0,5};
        if(arr1.length == arr2.length){
            System.out.println("Array is equal");
        }else{
            System.out.println("Array is not equal");
        }
    }
}

Output:

Array is equal

Explanations

This Java program checks if two arrays, arr1 and arr2, have the same number of elements. The arrays are predefined with numbers. The program starts by setting up a variable called sum to keep track of the total, which isn’t used in this comparison. The program then compares the lengths of the two arrays using arr1.length and arr2.length. If the lengths are the same, it prints “Array is equal” to the screen. If they are not the same, it prints “Array is not equal.” This simple check helps determine if the two arrays can be directly compared element by element, which could be useful in situations where operations depend on arrays of the same size

Program 2: Using Java Stream API

For more advanced programmers, Java’s Stream API can be used to compare arrays, especially when dealing with arrays of objects or when integrating with other stream operations.

public class Main {  
    public static void main(String[] args) {
        Integer[] array1 = {1, 2, 3, 4, 5};
        Integer[] array2 = {4, 5, 6, 7};

        boolean isEqual = Arrays.stream(array1).count() == Arrays.stream(array2).count();
        System.out.println("Are arrays equal in size? " + isEqual);
    }
}  

Output

Are arrays equal in size? false

Explanations

This Java program determines if two arrays, array1 and array2, are equal in size using Java’s Stream API. Both arrays are initialized with a set of integers, but they contain a different number of elements. The program uses Arrays.stream(array1).count() and Arrays.stream(array2).count() to count the elements in each array. These counts are then compared to see if they are the same. If the sizes of the two arrays are equal, the program prints “Are arrays equal in size? true” to the console. If the sizes are not equal, it prints “Are arrays equal in size? false”. This method provides a more modern approach to checking array sizes and can easily integrate with more complex stream operations.

Conclusion

Comparing the sizes of two arrays in Java is a fundamental task that can be accomplished in several ways, from simple property checks to more advanced stream-based methods. Whether you are working on simple data verification or complex data processing tasks, knowing how to check if two arrays are equal in size is crucial. These methods provide the flexibility to choose the right approach for your specific needs, making your Java programming more effective and reliable.

Leave a Comment