Compare Two Java long Arrays

Java 8Object Oriented ProgrammingProgramming

To compare two Java long arrays in Java, use Arrays.equals() method. Let’s say we have the following long arrays.

long[] arr1 = new long[] { 767, 568, 555, 897, 678 };
long[] arr2 = new long[] { 456, 756, 555, 999, 678};
long[] arr3 = new long[] { 767, 568, 555, 897, 678 };

Now, we can compare the equality of these arrays using the equals() method.

Arrays.equals(arr1, arr2);
Arrays.equals(arr2, arr3);
Arrays.equals(arr1, arr3);  

The following is the complete example.

Example

Live Demo

import java.util.*;
public class Demo {
   public static void main(String []args) {
      long[] arr1 = new long[] { 767, 568, 555, 897, 678 };
      long[] arr2 = new long[] { 456, 756, 555, 999, 678};
      long[] arr3 = new long[] { 767, 568, 555, 897, 678 };
      // comparing
      System.out.println(Arrays.equals(arr1, arr2));
      System.out.println(Arrays.equals(arr2, arr3));
      System.out.println(Arrays.equals(arr1, arr3));   
   }
}

Output

false
false
true
raja
Published on 05-Dec-2018 15:43:55
Advertisements