Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Set<String> equality ignore case

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?

like image 898
nafas Avatar asked Jul 03 '14 15:07

nafas


People also ask

How do I ignore a string case?

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.

Is set in Java case sensitive?

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).

Is set case sensitive?

If the set contains String elements, the elements are case-sensitive. Two set elements that differ only by case are considered distinct.


1 Answers

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)); } 
like image 188
Syam S Avatar answered Oct 05 '22 16:10

Syam S