Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make ArrayList element case-insensitive

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

List<String> a = new ArrayList<String>();
    a.add("one");
    a.add("three");
    a.add("two");
Collections.sort(a);


List<String> a1 = new ArrayList<String>();
    a1.add("ONE");
    a1.add("two");
    a1.add("THREE");
Collections.sort(a);

If i write a condition for equality it fails as some of list's element is in different case like

if(a.equals(a1)){
    System.out.println("equal");
} else{
    System.out.println("not equal");
}

It will display result "Not equal"

So please tell me how i can make the list element case-insensitive in Java language only.

Thank and regard

like image 640
Satya Avatar asked Aug 20 '10 07:08

Satya


People also ask

How do you make a case insensitive in Java?

Java String equalsIgnoreCase() Method The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not. Tip: Use the compareToIgnoreCase() method to compare two strings lexicographically, ignoring case differences.

How do you change an element in an ArrayList?

To replace an element in Java ArrayList, set() method of java. util. An ArrayList class can be used. The set() method takes two parameters-the indexes of the element which has to be replaced and the new element.

Are string cases insensitive?

A string column is case sensitive or not depending on the column's type.

What is case sensitive and case insensitive in Java?

Java is a case-sensitive language, which means in code showData and showdata are two different variables. Java is case-sensitive because it uses a C-style syntax. In most programming languages, case sensitivity is the norm. Case-sensitive is useful because it lets you infer what a name means based on its case.


4 Answers

You can also wrap your String into a helper class and implement the equals & compare methods for it.

public class StringWrapper implements Comparable<StringWrapper> {
    private String value;

    StringWrapper(Strig value) {
        this.value = value;
    }

    @Override boolean equals(Object o) {
        returns String.CASE_INSENSITIVE_ORDER.equals(
            (StringWrapper) o).value
            this.value);
    }

    @Override int compareTo(StringWrapper sw) {
        returns String.CASE_INSENSITIVE_ORDER.compare(
            this.value
            sw.value);        
    }

    @Override String toString() {
        return this.value;
    }

    @Override int hashCode() {
        return this.value.toLowerCase.hashCode();
    }
}

And then :

List<StringWrapper> a = new ArrayList<StringWrapper>();
    a.add(StringWrapper("one"));
    a.add(StringWrapper("TWO"));
    a.add(StringWrapper("three"));
Collections.sort(a);
like image 22
Raphael Jolivet Avatar answered Sep 30 '22 16:09

Raphael Jolivet


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.

like image 174
Riduidel Avatar answered Sep 30 '22 16:09

Riduidel


You need to use

Collections.sort(a, String.CASE_INSENSITIVE_ORDER);

in order to sort ignoring case, you can use the equalsIgnoreCase method on String to compare to values

You can of course create your own CaseInsensitiveList class, we have a CaseInsensitiveSet & CaseInsensitiveMap in our codebase

like image 45
Jon Freedman Avatar answered Sep 30 '22 15:09

Jon Freedman


You'd have to do that manually:

public boolean equalsIgnoreCase(List<String> l1, List<String> l2) {
  if (l1.size() != l2.size()) {
    return false;
  }
  Iterator<String> i1=l1.iterator();
  Iterator<String> i2=l2.iterator();
  while(i1.hasNext()) {
    if (!i1.next().equalsIgnoreCase(i2.next()) {
      return false;
    }
  }
  return true;
}
like image 37
Joachim Sauer Avatar answered Sep 30 '22 15:09

Joachim Sauer