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 OverflowYou 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.
Sensitive character filtering can use DFA algorithm, refer to: https://programmer.help/blogs/sensitive-word-filtering-aop-annotation-dfa-algorithm.html
If 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.
anyMatch only returns true or false indicating whether there is at least 1 element in the stream that satisfies the predicate.
You should use filter and findFirst, which returns the first element that matches the predicate:
speciality = specialities.stream()
.filter(spec::equalsIgnoreCase)
.findFirst()
.orElse("General Practitioner");
just include, speciality =spec.toLowerCase(); and convert the first character to uppercase
public void setSpeciality(String spec) {
if(specialities.stream().anyMatch(s-> s.equalsIgnoreCase(spec))) {
speciality =spec.toLowerCase();
speciality = String.valueOf((speciality.charAt(0)).toUpperCase()) + speciality.substring(1) ;
}
else {
speciality = "General Practitioner";
}
}
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);
This seems not to be (easily) possible with Streams alone1, but you can keep track of the already seen elements in a Set (O(1) lookup) and filter elements by whether their lowercased forms are already in that set (Set.add will return false then).
List<String> values = List.of("Value1", "vALue1", "vALue2", "valUE2");
Set<String> seen = new HashSet<>();
List<String> res = values.stream().filter(s -> seen.add(s.toLowerCase()))
.collect(Collectors.toList());
System.out.println(res); // [Value1, vALue2]
1) E.g., distinct does not accept a mapping function and Collectors.groupingBy might does not preserve order.
Some Java libraries which provide distinctBy functionality may be used to resolve this task.
For example, StreamEx library (GitHub, Maven Repo), which stands for Extenstion of Stream API, may be used like this:
import java.util.*;
import one.util.streamex.*;
public class MyClass {
public static void main(String args[]) {
String[] data = {
"Value1", "vALue1", "vALue2", "valUE2"
};
List<String> noDups = StreamEx.of(data)
.distinct(String::toLowerCase)
.toList();
System.out.println(noDups);
}
}
Output:
[Value1, vALue2]
There is no toUpper() method for String.
It is toUpperCase() and also you need to use contains() to check "BLACK" there anywhere in the whole string, so the code should be simple as shown below:
List<Employee> filtList = inputList.stream().
filter(value -> value.toUpperCase().//convert to uppercase for checking
contains("BLACK")).//filter values containing black
collect(Collectors.toList());//collect as list
Use regex:
List<Animal> filtList = list.stream()
.filter(x -> x.getName().matches("(?i).*black.*"))
.collect(Collectors.toList());
The regex flag "(?)" means "ignore case".
Try this.
public static void main(String[] args) {
List<Cat> cats = List.of(new Cat("たま", 3), new Cat("しろ", 2));
List<String> favoriteNames = List.of();
List<Cat> favoriteCats = cats.stream()
.filter(cat -> favoriteNames.isEmpty() || favoriteNames.contains(cat.getName()))
.toList();
System.out.println(favoriteCats);
}
output:
[Cat(name=たま, age=3), Cat(name=しろ, age=2)]
or
var stream = cats.stream();
if (!favoriteNames.isEmpty())
stream = stream.filter(cat -> favoriteNames.contains(cat.getName()));
List<Cat> favoriteCats = stream.toList();
public List<Cat> filterCats(List<Cat> input, List<String> favouriteNames) {
return input.stream()
.filter(cat -> favouriteNames.size() == 0 || favouriteNames.contains(cat.getName()))
.filter(cat -> cat.getAge() > 5)
.collect(Collectors.toList());
}
Where cat.getAge() > 5 is an example of an additional filter.
anyMatch will return true if some hobby matches.
List<Person> personsWithHobby = persons.stream()
.filter(person -> person.getHobbies().stream()
.anyMatch(searchHobby::contains))
.collect(Collectors.toList());
You can try:
persons.stream()
.filter(p -> p.getHobbies().stream()
.filter(searchHobby::contains)
.findAny().isPresent()
)
.collect(Collectors.toList());