Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check ArrayList<String> contains("") method with equalsIgnoreCase [duplicate]

I have an ArrayList();

List<String> list = new ArrayList<>();
list.add("aaa");
list.add("BBB");
list.add("cCc");
System.out.println(list.contains("aAa"));

Here i want to check contains() method with equalsIgnoreCase method in same line. How can i do it?

like image 913
Manoj Avatar asked Nov 30 '22 20:11

Manoj


2 Answers

boolean containsEqualsIgnoreCase(Collection<String> c, String s) {
   for (String str : c) {
      if (s.equalsIgnoreCase(str)) {
          return true;
      }
   }
   return false;
}
like image 139
jlordo Avatar answered Dec 06 '22 11:12

jlordo


You can't. The contract of contains is that it defers to equals. That's a fundamental part of the Collection interface. You have to write a custom method that iterates through the list and checks each value.

like image 38
Thorn G Avatar answered Dec 06 '22 09:12

Thorn G