If you're using Java 8
List<String> list = new ArrayList<>();
boolean containsSearchStr = list.stream().anyMatch("search_value"::equalsIgnoreCase);
Answer from Nivas Mane-Patil on Stack OverflowIf you're using Java 8
List<String> list = new ArrayList<>();
boolean containsSearchStr = list.stream().anyMatch("search_value"::equalsIgnoreCase);
I'm guessing you mean ignoring case when searching in a string?
I don't know any, but you could try to convert the string to search into either to lower or to upper case, then search.
// s is the String to search into, and seq the sequence you are searching for.
bool doesContain = s.toLowerCase().contains(seq);
Edit: As Ryan Schipper suggested, you can also (and probably would be better off) do seq.toLowerCase(), depending on your situation.
How To Ignore Case When Comparing ArrayList Elements To String ?
java - Checking if an ArrayList contains a certain String while being case insensitive - Stack Overflow
java - Array contains() without case sensitive lookup? - Stack Overflow
java - ArrayList contains case sensitivity - Stack Overflow
import java.util.ArrayList;
public class WordsCounter {
public static int occurences(String string, ArrayList<String> words){
int number = 0;
for(int i = 0; i < words.size(); i++){
if(words.get(i).equalsIgnoreCase(string)){
number++;
}
}
return number;
}
}I want to make a method that takes a string and a list of words as parameters, and returns how many of the words in the list could be found in the string at least once. It doesn't have to be the word exactly, just that a part of the string matches, like such:
If the String was "Sunshine is nice", and the list elements were "shine" and "nice", it would return 2.
I can't get it to work with the ignore of case, the program doesn't ignore case despite the method I used. I looked up String methods and equalsIgnoreCase seemed to be a good one but it does not work. I don't know if it's because it can't be applied to list elements (although they are Strings). Do any of you know how I should think regarding this?
You will need to iterate over the list and check each element. This is what is happening in the contains method. Since you are wanting to use the equalsIgnoreCase method instead of the equals method for the String elements you are checking, you will need to do it explicitly. That can either be with a for-each loop or with a Stream (example below is with a Stream).
private final List<String> list = new ArrayList<>();
public void addIfNotPresent(String str) {
if (list.stream().noneMatch(s -> s.equalsIgnoreCase(str))) {
list.add(str);
}
}
If you are using Java7, simply override the contains() method,
public class CastInsensitiveList extends ArrayList<String> {
@Override
public boolean contains(Object obj) {
String object = (String)obj;
for (String string : this) {
if (object.equalsIgnoreCase(string)) {
return true;
}
}
return false;
}
}
If you are using Java 8.0, using streaming API,
List<String> arrayList = new ArrayList<>();
arrayList.stream().anyMatch(string::equalsIgnoreCase);
contains just check if an object is present in the List. So you can't do a case insensitive lookup here, because "three" is a different object than "Three".
A simple approach to solve this would be
public boolean containsCaseInsensitive(String s, List<String> l){
for (String string : l){
if (string.equalsIgnoreCase(s)){
return true;
}
}
return false;
}
and then
containsCaseInsensitive("three", data);
Java 8+ version:
public boolean containsCaseInsensitive(String s, List<String> l){
return l.stream().anyMatch(x -> x.equalsIgnoreCase(s));
}
it will require you to write your own contains() method.
pointers for custom contains():
- iterate over all the elements in the list.
- compare the parameter string with all the values in
to make comparison case-insensitive be sure to apply any of toLowerCase or toUpperCase on BOTH parameter and the current element.
List list; public boolean contains(String str){
for(int i=0;i<list.size();i++){ if(list.elementAt(i).toLowerCase().Equals(str.toLowerCase())) return true; } return false; }
You can use this exactly like you'd use any other ArrayList. You can pass this List out to other code, and external code won't have to understand any string wrapper classes.
public class CustomStringList3 extends ArrayList<String> {
@Override
public boolean contains(Object o) {
String paramStr = (String)o;
for (String s : this) {
if (paramStr.equalsIgnoreCase(s)) return true;
}
return false;
}
}
In Java8, using anyMatch
List<String> list = Arrays.asList("XYZ", "ABC");
String matchingText = "xYz";
boolean isMatched = list.stream().anyMatch(matchingText::equalsIgnoreCase);