java - arraylist 检查元素是否在这里退出忽略大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12214576/
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
java - arraylist check if element exits here ignore the case
提问by naresh
Hi i have the arraylist it stores the string values. I want to check the some string is exists in the list or not. Here i want ignore the case sensitive.
嗨,我有存储字符串值的数组列表。我想检查列表中是否存在某个字符串。在这里我想忽略区分大小写。
code
代码
public static ArrayList< String > arrFoodItems = new ArrayList< String >();
if(arrFoodItems.contains("string"))
{
System.out.println("already available in the List.");
}
else
{
System.out.println("Not available");
}
It's working fine. But in the following example it fails. How to do it without using loops. Example: List contains: "Rice, Chicken,..." Now you are check for "rice" is exits in the list. In this case we are getting "not exists".
它工作正常。但在下面的例子中它失败了。如何在不使用循环的情况下做到这一点。示例:列表包含:“Rice, Chicken,...” 现在您正在检查列表中是否存在“rice”。在这种情况下,我们得到“不存在”。
Don't using this
不要使用这个
for(int i=0; i < arrFoodItems.size(); i++)
{
if(arrFoodItems.get(i)..equalsIgnoreCase(string))
{
System.out.println("already available in the List.");
}
}
回答by assylias
You could put all your strings in a TreeSet
with a custom Comparator that ignores the case:
您可以TreeSet
使用忽略大小写的自定义 Comparator将所有字符串放入 a 中:
List<String> list = Arrays.asList("Chicken", "Duck");
Set<String> set= new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
set.addAll(list);
System.out.println(set.contains("Chicken")); //true
System.out.println(set.contains("chicken")); //true
For more complex strategies / locale, you can also use a Collator:
对于更复杂的策略/语言环境,您还可以使用 Collator:
final Collator ignoreCase = Collator.getInstance();
ignoreCase.setStrength(Collator.SECONDARY);
List<String> list = Arrays.asList("Chicken", "Duck");
Set<String> set= new TreeSet<String>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return ignoreCase.compare(o1, o2);
}
});
set.addAll(list);
System.out.println(set.contains("Chicken"));
System.out.println(set.contains("chicken"));
回答by Peter Lawrey
Looping through the elements is what contains does for you so if you don't want to use a loop, its best not to use an ArrayList.
循环遍历元素是 contains 的作用,因此如果您不想使用循环,最好不要使用 ArrayList。
Without using a different collection your best option is to use
不使用不同的集合,你最好的选择是使用
FOUND: {
for(String item: arrFoodItems) {
if (item.equalsIgnoreCase(string)) {
System.out.println(string+" already available in the List.");
break FOUND;
}
}
System.out.println(string + " not available");
}
回答by Julien
You can use the Collections.binarySearch static method which let you provide a comparator ( in this case String.CASE_INSENSITIVE_ORDER)
您可以使用 Collections.binarySearch 静态方法,该方法可让您提供一个比较器(在本例中为 String.CASE_INSENSITIVE_ORDER)
List<String> list = new ArrayList<String>();
list.add("Joe");
Collections.binarySearch(list, "joe", String.CASE_INSENSITIVE_ORDER); // return 0
Hope this helps!
希望这可以帮助!
回答by amicngh
Why don't just write your CustomArrayList
and then override contains
method like below. Now you can use CustomArrayList
in place of ArrayList
.
为什么不直接编写您的CustomArrayList
然后重写contains
方法,如下所示。现在您可以使用CustomArrayList
代替ArrayList
.
Example:
例子:
public class CustomArrayList extends ArrayList<String> {
@Override
public boolean contains(Object o) {
String str = (String)o;
for (String s : this) {
if (str.equalsIgnoreCase(s)) return true;
}
return false;
}
@Test
public static void main(String[] args) {
List<String> myList=new CostumArrayList();
myList.add("Amit");
System.out.println(myList.contains("AMIT"));
System.out.println(myList.contains("amit"));
}
}