You could use toLowerCase():

u -> u.name1.toLowerCase().contains(keyword.toLowerCase());

This way both values are in the same case and your search is case in-sensitive.

Answer from JDC on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › case-insensitive string matching in java
Case-Insensitive String Matching in Java | Baeldung
August 28, 2025 - In this tutorial, we looked at a few different ways to check a String for a substring, while ignoring the case in Java. We looked at using String.toLowerCase() and toUpperCase(), String.matches(), String.regionMatches(), Apache Commons StringUtils.containsIgnoreCase(), and Pattern.matcher().find().
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Case Insensitive String Handling in Java Lists - Java Code Geeks
February 28, 2024 - It checks whether the current element (str) of the stream when compared to ignoring case (equalsIgnoreCase), is equal to the element. The result of anyMatch is returned, which is true if at least one element in the list matches the given element ...
🌐
Baeldung
baeldung.com › home › java › java string › case-insensitive searching in arraylist
Case-Insensitive Searching in ArrayList | Baeldung
March 7, 2025 - It’s available on Java 8 and later versions. For example, we can use Stream‘s anyMatch() method to do a case-insensitive string search:
Top answer
1 of 1
5

There are several fundamental problems with your code.

  • .filter(s -> word.contains(s)) performs a substring search. Contrary to your question’s title, it does not ignore case. Still, there can be strings of different content passing the filter

  • .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) creates groups according to the string’s actual content. So when multiple different strings passed the previous filter, multiple groups may exist

  • .values().stream().findFirst(): since the groupingBy created a map with an unspecified ordering, this will pick an arbitrary group. Besides that, it’s a very inefficient way to ask just for the count()

  • .orElse((long) -1) The value -1 is a very strange fall-back for counting, as the most natural answer would be “zero” when there are no matches.

So a straight-forward solution would be

public static long countWordOccurence(List<String> wordList, String word) {
    return Collections.frequency(wordList, word);
}

for counting case sensitive matches or

public static long countWordOccurence(List<String> wordList, String word) {
    return wordList.stream().filter(word::equalsIgnoreCase).count();
}

for counting case insensitive.

But that’s an xy problem anyway.

When you want to count occurrences of a word in a string, it’s not necessary to split the string into words and to convert the array into a list (by the way, you can stream over an array directly), before performing the actual search.

You can use

public static long countWordOccurence(String sentence, String word) {
    if(!word.codePoints().allMatch(Character::isLetter))
        throw new IllegalArgumentException(word+" is not a word");
    Pattern p = Pattern.compile("\\b"+word+"\\b");
    return p.matcher(sentence).results().count();
}

for a count of case sensitive matches and

public static long countWordOccurence(String sentence, String word) {
    if(!word.codePoints().allMatch(Character::isLetter))
        throw new IllegalArgumentException(word+" is not a word");
    Pattern p = Pattern.compile("\\b"+word+"\\b", Pattern.CASE_INSENSITIVE);
    return p.matcher(sentence).results().count();
}

for the case insensitive matches. The \b pattern denotes word boundaries, which only makes sense if the search string is actually a word. So the methods above have a pre-test for that, which also ensures that the word does not contain characters that could be misinterpreted as regex patterns.

The results() method was introduced in Java 9. This answer shows a solution for creating such a stream under Java 8, however, for such a simple task as counting the occurrences, the alternative would be not to use streams here:

public static long countWordOccurence(String sentence, String word) {
    if(!word.codePoints().allMatch(Character::isLetter))
        throw new IllegalArgumentException(word+" is not a word");
    Pattern p = Pattern.compile("\\b"+word+"\\b", Pattern.CASE_INSENSITIVE);
    int count = 0;
    for(Matcher m = p.matcher(sentence); m.find(); count++) {}
    return count;
}
🌐
O'Reilly
oreilly.com › library › view › javatm-how-to › 9780133813036 › ch17lev2sec18.html
17.5.2 Filtering Strings Then Sorting Them in Case-Insensitive Ascending Order - Java™ How To Program (Early Objects), Tenth Edition [Book]
17.5.2 Filtering Strings Then Sorting Them in Case-Insensitive Ascending Order Lines 24–28 filter and sort the Strings. Line 25 creates a Stream<String> from the array... - Selection from Java™ How To Program (Early Objects), Tenth Edition [Book]
Find elsewhere
🌐
Websparrow
websparrow.org › home › check hashset contains element case insensitive in java
Check HashSet contains element case insensitive in Java - Websparrow
July 14, 2019 - package org.websparrow; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class HashSetContains { public static void main(String[] args) { Set<String> cars = new HashSet<>(); cars.add("Tata"); cars.add("mAHinDrA"); cars.add("BMW"); cars.add("Maruti Suzuki"); /** * Using Set contains method */ System.out.println(cars.contains("Tata")); // true System.out.println(cars.contains("TATA")); // false /** * Using Java 8 */ // matching equality -> Case sensitive boolean containsMyCar = cars.stream().anyMatch("Tata"::equals); System.out.println(containsMyCar); // true boo
🌐
Baeldung
baeldung.com › home › java › java list › check if a list contains a string element while ignoring case
Check if a List Contains a String Element While Ignoring Case | Baeldung
March 7, 2025 - However, in many cases, we want the contains() method to support case-ignored checks. Unfortunately, the standard contains() doesn’t offer us this option.
🌐
W3Schools
w3schools.com › java › ref_string_equalsignorecase.asp
Java String equalsIgnoreCase() Method
Java Examples Java Videos Java ...yStr1.equalsIgnoreCase(myStr3)); // false ... The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences....
🌐
Java2s
java2s.com › Tutorials › Java › Stream_How_to › Stream_Filter › Filter_String_stream_and_map_to_upper_case_then_sort.htm
Java Stream How to - Filter String stream and map to upper case then sort
We would like to know how to filter String stream and map to upper case then sort. /* ww w . j av a 2s.c o m*/ import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { Arrays.asList("a1", "a2", "b1", "c2", "c1") .stream() .filter(s -> s.startsWith("c")) .map(String::toUpperCase) .sorted() .forEach(System.out::println); } } The code above generates the following result.
🌐
Educative
educative.io › answers › what-is-stringutilscontainsignorecase-in-java
What is StringUtils.containsIgnoreCase in Java?
containsIgnoreCase is a static method of the StringUtils class that checks whether a given string contains the complete search string as a whole, while ignoring the case considerations.