Since the equals function in array only check the instance, it doesn't work well with Set. Hence, I wonder how to make a set of arrays in java?
One possible way could be put each array in an object, and implement equals function for that class, but will that decrease the performance too much?
Don't use raw Arrays unless you absolutely have to because of some legacy API that requires an Array.
Always try and use a type safe
ArrayList<T>
instead and you won't have these kind of issues.Since the ArrayList class already wraps an array, you can extend it and override the
equals
andhashCode
methods. Here is a sample:UPDATE:
You can even override it for a generic use, just changing the code to:
You could create a wrapper class for your array and override hashcode and equals accordingly. For example:
A class that extends Set and override the equals method could do it.
If you make your Set be an instance of TreeSet, you can specify a custom Comparator which will be used for all comparisons (even equality).