I want to check if all elements of two sets of String are equal by ignoring the letter's cases.
Set<String> set1 ; Set<String> set2 ; . . . if(set1.equals(set2)){ //all elements of set1 are equal to set2 //dosomething } else{ //do something else }
However, this equality check doesn't ignore the cases of the string. Is there some other way of doing that?
Java String equalsIgnoreCase() Method with ExamplesThe equalsIgnoreCase() method of the String class compares two strings irrespective of the case (lower or upper) of the string. This method returns a boolean value, true if the argument is not null and represents an equivalent String ignoring case, else false.
Java, like most programming languages, is case sensitive. Even the slightest difference in naming indicates different objects (count does not equal Count). In order to be consistent, programmers follow naming conventions. For example, variables are lowercase (car) and classes are uppercase (Car).
If the set contains String elements, the elements are case-sensitive. Two set elements that differ only by case are considered distinct.
Alternatively you can use TreeSet
.
public static void main(String[] args){ Set<String> s1 = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); s1.addAll(Arrays.asList(new String[] {"a", "b", "c"})); Set<String> s2 = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); s2.addAll(Arrays.asList(new String[] {"A", "B", "C"})); System.out.println(s1.equals(s2)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With