简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM