java 使 ArrayList 元素不区分大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3529025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make ArrayList element case-insensitive
提问by Satya
In my Java application i need to compare two list's element whether it is similar or not.
在我的 Java 应用程序中,我需要比较两个列表的元素是否相似。
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.
所以请告诉我如何使列表元素仅在 Java 语言中不区分大小写。
Thank and regard
感谢和关注
回答by Riduidel
Why not using instead a SortedSet with a case insensitive comparator ? With the String.CASE_INSENSITIVE_ORDERcomparator
为什么不使用带有不区分大小写的比较器的 SortedSet 呢?使用String.CASE_INSENSITIVE_ORDER比较器
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
并且您的平等条件应该可以正常工作
EDITmodified according to comments. Thanks to all of you to correct me.
编辑根据评论修改。谢谢大家指正。
回答by Jon Freedman
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
为了排序忽略大小写,您可以使用 String 上的 equalsIgnoreCase 方法与值进行比较
You can of course create your own CaseInsensitiveList class, we have a CaseInsensitiveSet & CaseInsensitiveMap in our codebase
您当然可以创建自己的 CaseInsensitiveList 类,我们的代码库中有 CaseInsensitiveSet 和 CaseInsensitiveMap
回答by Joachim Sauer
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;
}
回答by Raphael Jolivet
You can also wrap your String into a helper class and implement the equals& comparemethods for it.
您还可以将您的 String 包装到一个辅助类中并为其实现equals& compare方法。
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);
回答by Johannes Wachter
How about writing a Wrapper class to the List you are using, this would avoid to have inconsistent storage of the elements.
将 Wrapper 类写入您正在使用的 List 怎么样,这将避免元素的不一致存储。
public class CaseInsensitiveStringList extends ArrayList<String> {
@Override
public void add(final int index, final String element) {
super.add(index, element.toLowerCase());
}
@Override
public boolean add(final String o) {
return super.add(o.toLowerCase());
}
@Override
public boolean addAll(final Collection<? extends String> c) {
final ArrayList<String> temp = new ArrayList<String>(c.size());
for (final String s : c) {
temp.add(s.toLowerCase());
}
return super.addAll(temp);
}
@Override
public boolean addAll(final int index, final Collection<? extends String> c) {
final ArrayList<String> temp = new ArrayList<String>(c.size());
for (final String s : c) {
temp.add(s.toLowerCase());
}
return super.addAll(index, temp);
}
}
回答by Harshita Sethi
To sort list of Strings ignoring case
对字符串列表进行排序忽略大小写
Arrays.sort(myArray, Collator.getInstance());
回答by Thorbj?rn Ravn Andersen
You will need to override the equals() method on the list so it does what you want. Have a look at the current ArrayList.equals() and adapt it so it compares with equalsIgnoreCase instead of equals().
您将需要覆盖列表上的 equals() 方法,以便它执行您想要的操作。查看当前的 ArrayList.equals() 并对其进行调整,使其与 equalsIgnoreCase 而不是 equals() 进行比较。

