The following Java code:
public static void main(String args[]) {
int[] x = new int[] {1, 2, 3};
int[] y = new int[] {1, 2, 3};
LinkedList<int[]> list = new LinkedList<int[]>();
list.add(x);
System.out.println("List contains y: " + list.contains(y));
}
gives the output
List contains y: false
which makes sense as x and y are references to different memory locations, however there is also a sense in which they are equal (they have the the same elements in the same order).
Is there a data structure which would return true to the query list.contains(y) in this example?
equalsas desired -- in which case it will work as expected, albeit a bit more work. Some of the data-structures (HashMap, for instance) do allow one to specify a custom comparable.isSubsetOf,isSubstringOfor something else? ByisSubstringOfI mean the exact array should be a part of the original array.