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
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 507383 โ€บ java โ€บ list-element-case-insensitive
Make list element case-insensitive (Java in General forum at Coderanch)
August 20, 2010 - I meant both equalsIgnoreCase and the built-in Comparator in the String class, Joanne; you need that Comparator, otherwise it will sort "ONE" "THREE" "two". As you say, you would have to write your own method using it equalsIgnoreCase. And yes, just like you, I thought it meant comparing the Strings.
Discussions

java - Is there a clean way to ignore case when comparing List<String>? - Stack Overflow
Since you are comparing sizes, I'll assume that order is relevant, as lists are ordered collections. The code shown in the question ignores order. To compare all elements case-insensitively, in order, you should parallel iterate both lists. More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - ArrayList contains case sensitivity - Stack Overflow
I am currently using the contains method belonging to the ArrayList class for making a search. Is there a way to make this search case insensitive in java? I found that in C# it is possible to use More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 3, 2016
java - Option to ignore case with .contains method? - Stack Overflow
Is there an option to ignore case with .contains() method? I have an ArrayList of DVD object. Each DVD object has a few elements, one of them is a title. And I have a method that searches for a sp... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How To Ignore Case When Comparing ArrayList Elements To String ?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/javahelp
7
4
December 9, 2022
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_string_equalsignorecase.asp
Java String equalsIgnoreCase() Method
Java Examples Java Videos Java ...yStr1.equalsIgnoreCase(myStr3)); // false ... The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences....
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ how to compare two lists in java
How to Compare Two Lists in Java - HowToDoInJava
September 20, 2023 - public ArrayList sortDescending(){ Collections.sort(this.arrayList,String.CASE_INSENSITIVE_ORDER); Collections.sort(this.arrayList,Collection.reverseOrder()); return this.arrayList; } Reply ... A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
Top answer
1 of 3
4

Since you are comparing sizes, I'll assume that order is relevant, as lists are ordered collections. The code shown in the question ignores order.

To compare all elements case-insensitively, in order, you should parallel iterate both lists. Since get(int) is not fast for all List implementations, a parallel iteration using Iterator is best:

private static boolean equalsIgnoreCase(List<String> list1, List<String> list2) {
    if (list1.size() != list2.size())
        return false;
    for (Iterator<String> iter1 = list1.iterator(), iter2 = list2.iterator(); iter1.hasNext(); )
        if (! iter1.next().equalsIgnoreCase(iter2.next()))
            return false;
    return true;
}
2 of 3
0

I am assuming you want to compare 2 lists.

Ensure neither list is null.

if(list1 == null || list2 == null) return false;

Check sizes.

if(list1.size() != list2.size()) return false;

Check elements in lists (including the ordering of strings(?), but not casing)

for(int i = 0 ; i < list1.size() ; i++) {
    // i think equalsIgnoreCase takes care of null strings
    // if it doesn't, do it yourself :)
    String first = list1.get(i);
    String second = list2.get(i);
    if(first == null && second != null)
        return false;
    if(first == null && second == null)
        continue;
    if( !(first.equalsIgnoreCase(second)) )
        return false;
}

If you got here. Everything is probably good.

return true;

As Andreas noted, for lists that are implemented like LinkedList the get(..) gives bad performance. An iterator based approached like his answer solves that issue.

๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ lang โ€บ String โ€บ compareToIgnoreCase
Java String compareToIgnoreCase() - Compare Ignoring Case | Vultr Docs
November 19, 2024 - The compareToIgnoreCase() method in Java provides a straightforward, powerful tool for case-insensitive string comparisons. It enhances functionality in sorting and searching operations where case sensitivity is irrelevant.
Find elsewhere
๐ŸŒ
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 ... provides a more elegant way to perform operations on collections. We can utilize the anyMatch() method along with a case-insensitive comparison ......
๐ŸŒ
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....
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_string_comparetoignorecase.asp
Java String compareToIgnoreCase() Method
Java Examples Java Videos Java ....compareToIgnoreCase(myStr2)); ... The compareToIgnoreCase() method compares two strings lexicographically, ignoring lower case and upper case differences....
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ how to ignore case when comparing arraylist elements to string ?
r/javahelp on Reddit: How To Ignore Case When Comparing ArrayList Elements To String ?
December 9, 2022 -
import java.util.ArrayList;

public class WordsCounter {

    public static int occurences(String string, ArrayList<String> words){
        int number = 0;
        for(int i = 0; i < words.size(); i++){
            if(words.get(i).equalsIgnoreCase(string)){
                number++;
            }
        }
        return number;
    }
}

I want to make a method that takes a string and a list of words as parameters, and returns how many of the words in the list could be found in the string at least once. It doesn't have to be the word exactly, just that a part of the string matches, like such:

If the String was "Sunshine is nice", and the list elements were "shine" and "nice", it would return 2.

I can't get it to work with the ignore of case, the program doesn't ignore case despite the method I used. I looked up String methods and equalsIgnoreCase seemed to be a good one but it does not work. I don't know if it's because it can't be applied to list elements (although they are Strings). Do any of you know how I should think regarding this?

๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-string-equalsignorecase
Java String equalsIgnoreCase() method - javatpoint
Java String equalsIgnoreCase() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string equalsignorecase in java etc.
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ comparetoignorecase-java
Mastering `compareToIgnoreCase` in Java โ€” javaspring.net
In Java, string comparison is a ... regardless of their case. The `compareToIgnoreCase` method is a powerful tool provided by the `String` class to perform such case-insensitive string comparisons....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-do-we-make-my-string-comparison-case-insensitive-in-java
How do we make my string comparison case insensitive in java?
October 11, 2019 - Using the equals() method โˆ’ of the String class accepts a String as parameter and it compares the current string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object including case.
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ string โ€บ java string equalsignorecase()
Java String equalsIgnoreCase() with Examples - HowToDoInJava
January 6, 2023 - equals() method does case-insensitive comparison. equalsIgnoreCase() method does case-insensitive comparison. the above last two line have two methods(equals() , equalsIgnoreCase()) having same meaning ???????is it correct Reply ยท It was type error.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ java-string-compare
Comparing Strings in Java: Methods and Tips
February 26, 2024 - While equals(), compareTo(), and their case-insensitive counterparts are the most common methods for string comparison in Java, there are other alternatives. These include the == operator, contentEquals(), regionMatches(), and certain third-party ...