[Solved] JAVA – Getting differen between two arrays

Introduction

Java is a powerful programming language that can be used to solve a variety of problems. One such problem is getting the difference between two arrays. This can be done in a few different ways, depending on the type of data in the arrays and the desired output. In this article, we will discuss the various methods of getting the difference between two arrays in Java. We will also discuss the advantages and disadvantages of each approach. Finally, we will provide some examples of code to help you get started.

Solution

public static int[] getDifference(int[] arr1, int[] arr2) {
Set set1 = new HashSet<>();
Set set2 = new HashSet<>();
for (int i : arr1) {
set1.add(i);
}
for (int i : arr2) {
set2.add(i);
}
set1.removeAll(set2);
int[] result = new int[set1.size()];
int i = 0;
for (int n : set1) {
result[i++] = n;
}
return result;
}

First you would need to make a method in your custom class that accepts an object of the same class and tests whether the imputed object has the same values as the object which is calling the method. Then just loop through the array and see if the object is equal to another object. This has already been done in the String class. In the String class there is a method
equals(String s) which you could use

Here is an example of how you would compare 2 String arrays. To make it work with you custom class, just change the array type and make a .equals method in your class.

String[] array1={"Hello","Hi"};
String[] array2={"Hello","eat"};

for(int x=0;x<array1.length;x++){
    Boolean present=false;
    for(int y=0;y<array2.length;y++){
       if(array1[x].equals(array2[y])){
         present=true;
         break;
       }
     }
   if(!present){
      System.out.println(array1[x]);
 }
} 

EDIT:

here is how you would make your equals method in your custom class

public class custom{
int value;
String text;

public Boolean equals(custom obj){
  if(this.value==obj.value && this.text.equals(obj.text)){
  return true;
 }else{return false;}
}
}

1

solved JAVA – Getting differen between two arrays

Solved: Java – Getting the Difference Between Two Arrays

When working with arrays in Java, it is often necessary to compare two arrays and determine the differences between them. This can be a tricky task, but luckily there are a few methods that can be used to make the process easier. In this article, we will discuss how to get the difference between two arrays in Java.

Using the Arrays.equals() Method

The Arrays.equals() method is a convenient way to compare two arrays and determine if they are equal. This method takes two arrays as parameters and returns a boolean value indicating whether or not the arrays are equal. To use this method, simply pass in the two arrays as parameters and check the return value.

boolean areEqual = Arrays.equals(array1, array2);
if (areEqual) {
    // arrays are equal
} else {
    // arrays are not equal
}

Using the Arrays.deepEquals() Method

The Arrays.deepEquals() method is similar to the Arrays.equals() method, but it is designed to compare arrays of objects. This method takes two arrays as parameters and returns a boolean value indicating whether or not the arrays are equal. To use this method, simply pass in the two arrays as parameters and check the return value.

boolean areEqual = Arrays.deepEquals(array1, array2);
if (areEqual) {
    // arrays are equal
} else {
    // arrays are not equal
}

Using the Arrays.difference() Method

The Arrays.difference() method is a convenient way to compare two arrays and determine the differences between them. This method takes two arrays as parameters and returns an array containing the elements that are present in the first array but not in the second. To use this method, simply pass in the two arrays as parameters and check the return value.

int[] difference = Arrays.difference(array1, array2);
for (int element : difference) {
    // element is present in array1 but not in array2
}

Conclusion

In this article, we discussed how to get the difference between two arrays in Java. We looked at three different methods that can be used to compare two arrays and determine the differences between them. With these methods, it is easy to compare two arrays and determine the differences between them.