Why not using instead a SortedSet with a case insensitive comparator ? With the String.CASE_INSENSITIVE_ORDER comparator

Your code is reduced to

Set<String> a = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
    a.add("one");
    a.add("three");
    a.add("two");


Set<String> a1 = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
    a1.add("ONE");
    a1.add("two");
    a1.add("THREE");

And your equals conditions should work without any issue

EDIT modified according to comments. Thanks to all of you to correct me.

Answer from Riduidel on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › case-insensitive searching in arraylist
Case-Insensitive Searching in ArrayList | Baeldung
March 7, 2025 - In this tutorial, we’ll explore how to search a string in an ArrayList<String> object case-insensitively. Internally, the ArrayList.contains() method uses the equals() method to determine whether the list has a given element.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Case Insensitive String Handling in Java Lists - Java Code Geeks
February 28, 2024 - The above method Iterates through each element in the list and compares it with the search string after converting both to lowercase. The Java Stream API provides a more elegant way to perform operations on collections. We can utilize the anyMatch() method along with a case-insensitive comparison to check if the list contains a string element:
🌐
Coderanch
coderanch.com › t › 507383 › java › list-element-case-insensitive
Make list element case-insensitive (Java in General forum at Coderanch)
August 20, 2010 - In my java application i need to compare two list's element whether it is similar or not. In short suppose i have two list declared like shown below If i write a condition for equality it fails as some of list's element is in different case like It will display result "Not equal" So please tell me how i can make the list element case-insensitive in java language only.
🌐
Baeldung
baeldung.com › home › java › java list › check if a list contains a string element while ignoring case
Check if a List Contains a String Element While Ignoring Case | Baeldung
March 7, 2025 - Under the hood, List.contains() compares the given object with each element in the list through the equals() method. Therefore, if the list is a List<String>, the contains() method only compares strings case-sensitively.
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-sort-a-list-in-case-insensitive-order
Java Program to sort a List in case insensitive order
July 30, 2019 - Collections.sort(list, String.CASE_INSENSITIVE_ORDER); Live Demo · import java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo { public static void main(String[] argv) throws Exception { String[] arr = new String[] { "P", "W", "g", "K", "H", "t", "E" }; List<String>list = Arrays.asList(arr); System.out.println("List = "+list); Collections.sort(list); System.out.println("Case Sensitive Sort = "+list); Collections.sort(list, String.CASE_INSENSITIVE_ORDER); System.out.println("Case Insensitive Sort = "+list); } } List = [P, W, g, K, H, t, E] Case Sensitive Sort = [E, H, K, P, W, g, t] Case Insensitive Sort = [E, g, H, K, P, t, W] Samual Sam ·
🌐
Coderanch
coderanch.com › t › 687154 › java › HashSet-case-insensitive
HashSet case insensitive (Java in General forum at Coderanch)
November 14, 2017 - If you want to prevent the creation of new Strings you can create some utility methods (e.g. equalsLowerCase(String s1, String s2), hashCodeLowerCase(String s) and possibly even compareToLowerCase(String s1, String s2)). I was looking at Java's source code for equalsIgnoreCase() and they do an interesting thing, they compare the chars to see if they're equal, if not then they compare the lower case of the chars to see if they're equal.
Find elsewhere
🌐
amitph
amitph.com › home › java › case-insensitive search in java arraylists
Case-Insensitive Search in Java ArrayLists - amitph
November 22, 2024 - public static boolean ... false; }Code language: Java (java) The method iterates over the given List and tries to match elements with the given text, ignoring the case....
🌐
Java2s
java2s.com › Tutorials › Java › Collection_How_to › List › Sort_String_list_in_case_insensitive_order.htm
Java Collection How to - Sort String list in case insensitive order
We would like to know how to sort String list in case insensitive order. //w w w . j a va2 s . c om import java.util.Arrays; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] argv) throws Exception { String[] strArray = new String[] { "z", "a", "java2s.com" }; List list = Arrays.asList(strArray); Collections.sort(list); Collections.sort(list, String.CASE_INSENSITIVE_ORDER); Collections.sort(list, Collections.reverseOrder()); Collections.sort(list, String.CASE_INSENSITIVE_ORDER); Collections.reverse(list); } } The code above generates the following result.
🌐
Roy Tutorials
roytuts.com › home › java › searching an element in java arraylist
Searching an element in Java ArrayList - Roy Tutorials
September 27, 2016 - package com.roytuts.java.arraylist; import java.util.ArrayList; import java.util.List; public class SearchInArrayListCaseInsensitive { private boolean contains(String str, List<String> list) { for (String s : list) { if (str.equalsIgnoreCase(s)) return true; } return false; } private int indexOf(String str, List<String> list) { if (str != null) { for (int i = 0; i < list.size(); i++) { if (str.equalsIgnoreCase(list.get(i))) { return i; } } } return -1; } public static void main(String[] args) { SearchInArrayListCaseInsensitive insensitive = new SearchInArrayListCaseInsensitive(); List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); boolean found = insensitive.contains("a", list); System.out.println("element a : " + (found ?
🌐
Overclock.net
overclock.net › home › forums › software, programming and coding › coding and programming
Java List.Contains -- Case Insensitive? | Overclock.net
June 20, 2008 - Is there a way to use List.Contains(var) in case-insensitive form? E.g. if the list had "LyokoHaCk" and I plugged in "lyokohack" for the variable, it would still return true. I've tried iterating this and converting it to a string array then looping, but no luck! Thanks for the help!
🌐
Baeldung
baeldung.com › home › java › java string › case-insensitive string matching in java
Case-Insensitive String Matching in Java | Baeldung
August 28, 2025 - The matches() method takes a String to represent the regular expression. (?i) enables case-insensitivity and .* uses every character except line breaks.
🌐
Websparrow
websparrow.org › home › check hashset contains element case insensitive in java
Check HashSet contains element case insensitive in Java - Websparrow
July 14, 2019 - package org.websparrow; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class HashSetContains { public static void main(String[] args) { Set<String> cars = new HashSet<>(); cars.add("Tata"); cars.add("mAHinDrA"); cars.add("BMW"); cars.add("Maruti Suzuki"); /** * Using Set contains method */ System.out.println(cars.contains("Tata")); // true System.out.println(cars.contains("TATA")); // false /** * Using Java 8 */ // matching equality -> Case sensitive boolean containsMyCar = cars.stream().anyMatch("Tata"::equals); System.out.println(containsMyCar); // true boo
🌐
javaspring
javaspring.net › blog › ignorecase-java
Mastering Case-Insensitive Operations in Java — javaspring.net
This code sorts the list of strings in a case-insensitive order. When validating user input, it is often necessary to perform case-insensitive checks. For example, validating if a user entered a valid option from a set of choices. import java.util.Scanner; public class UserInputValidation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter 'yes' or 'no':"); String userInput = scanner.nextLine(); if ("yes".equalsIgnoreCase(userInput)) { System.out.println("You selected yes."); } else if ("no".equalsIgnoreCase(userInput)) { System.out.println("You selected no."); } else { System.out.println("Invalid input."); } scanner.close(); } }
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › case-insensitive-search
Case Insensitive Search - GeeksforGeeks
July 23, 2025 - // Java Program for Case Insensitive Search by matching // with all substrings import java.util.ArrayList; import java.util.List; class GfG { // Method for case insensitive search by matching with all substrings static ArrayList<Integer> search(String txt, String pat) { ArrayList<Integer> res = new ArrayList<>(); int n = txt.length(); int m = pat.length(); // Match the pattern with all substrings of txt for (int i = 0; i < n - m + 1; i++) { boolean found = true; for (int j = 0; j < m; j++) { // Convert the characters to lower case if (Character.toLowerCase(txt.charAt(i + j)) != Character.toLow