raja pandian
raja pandian

Reputation: 9

LIKE Predicate in Java. Does it Exist?

I have a requirement in which we have to search an object from a list of it. For example if we have a list of Persons object

List<User> userList = new ArrayList<User>();

The User object would have first name and last name as parameters like this.

 public class User{
    String firstName;
    String lastName;
  }

I need to perform a search in which if the user enters "raj" as input it should produce the same result as "LIKE" query.

I have used apache-commons predicate for doing. it worked well for "EQUALS". But do we have any other class in apache-commons which can be used for LIKE queries.

Upvotes: 0

Views: 737

Answers (1)

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

You mean

userList
  .stream()
  .filter(user -> user.getFirstName().contains(input))
  .collect(Collectors.toList());

?

String#contains will return true if... I don't really think I have to explain that.

This would return a list of all users where firstname contains the input.

Upvotes: 2

Related Questions