I'm wondering if there is a way to easily modify the ".contains()" method in the List interface in Java without creating a custom class. For example:
When dealing with a collection of arrays in java, the .contains() method will always return false because the .contains() method always checks for equality with a generic .equals() call from the Object class that only returns true if the compared objects have the same reference in memory. However, with arrays, it is much more useful to do an Arrays.equals() check on the two arrays.
Code:
public class Temp {
public static void main(String[] args) {
int[] myIntArray = new int[] {1, 2, 3};
List<int[]> myList = new ArrayList<>();
myList.add(myIntArray);
System.out.println(myList.contains(new int[] {1, 2, 3}));
} // Output in the console: false
// Preferred output: true
}
I understand that it is possible to do this rather easily by using a for loop and iterating over the whole list using the Arrays.equals() method, but the goal for me is to learn how to easily sculpt the .contains() method into what I need for future use. Thanks a lot!