I want to check if all elements of two sets of String are equal by ignoring the letter\'s cases.
Set set1 ;
Set set2 ;
.
.
.
if(s
Not that I know of.
The best solution I can see, albeit over-engineered, would be to create your custom holder class holding a String
instance field (String
is final
and cannot be inherited).
You can then override equals
/ hashCode
wherein for two String
properties equalsIgnoreCase
across two instances, equals
would return true
and hashCode
s would be equal.
This implies:
hashCode
returns a hash code based on a lower (or upper) cased
property's hash code.equals
is based on equalsIgnoreCase
class MyString {
String s;
MyString(String s) {
this.s = s;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((s == null) ? 0 : s.toLowerCase().hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyString other = (MyString) obj;
if (s == null) {
if (other.s != null)
return false;
}
else if (!s.equalsIgnoreCase(other.s))
return false;
return true;
}
}
public static void main(String[] args) {
Set set0 = new HashSet(
Arrays.asList(new MyString[]
{
new MyString("FOO"), new MyString("BAR")
}
)
);
Set set1 = new HashSet(
Arrays.asList(new MyString[]
{
new MyString("foo"), new MyString("bar")
}
)
);
System.out.println(set0.equals(set1));
}
Output
true
... as said, over-engineered (but working).