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
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.
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.
A string column is case sensitive or not depending on the column's type.
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.
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);
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.
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
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;
}
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