Arrays don't have a "natural order", that's why you got this exception.
One way is to provide a custom comparator when constructing your TreeSet
.
Set<String[]> s = new TreeSet<String[]>(new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
//your logic here
}
});
But I don't think you should use a TreeSet
for that sort of things because that will be hard to tell how to define an order for comparing your arrays.
IMO your best option is to create a wrapper class, overriding hashcode and equals and put those in an HashSet
.
class WrapperStringArray {
private String[] arr;
//constructors, getters, setter and additional methods
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(arr);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
WrapperStringArray other = (WrapperStringArray) obj;
return Arrays.equals(arr, other.arr);
}
}