JAVA 8 filter list of object with any matching property

11,751

Solution 1

You can solve this by using reflection to read all class properties

or

By overriding toString() method and check if the result of to String contains input string

Solution 2

One possible way would be to override toString() with required properties and use it to check if it contains a word,

contactList.stream()
    .filter(contact -> contact.toString().contains("dubai"))
    .collect(Collectors.toList());

You may also want to use Pattern to match the exact word.

Solution 3

You can use findAny. This will end as soon as the candidate is found:

Optional<Contact> contact = contactList.stream().filter(contact -> contact.getStreet().equals("dubai") || 
                        contact.getCity().equals("dubai") || .......).findAny();

Or if you only need an information if such an object exists, use anyMatch:

boolean exists = contactList.stream().anyMatch(contact -> contact.getStreet().equals("dubai") || 
                        contact.getCity().equals("dubai") || .......);
Share:
11,751
Mujahid
Author by

Mujahid

Updated on June 04, 2022

Comments

  • Mujahid
    Mujahid almost 2 years

    My requirement is to filter a list of objects by a string matching any of the properties. For example, let say Contact class has three properties: street, city, phone.

    I am aware of how java stream filter works, where i have to compare the input string with each property as below:

    contactList.stream().filter(contact -> contact.getStreet().equals("dubai") || 
                                contact.getCity().equals("dubai") || .......).collect(Collectors.toList());
    

    However, if a class has 20+ properties and size of list 80,000+, comparing each property with the input string would affect the performance. So my question is, does Java or any other library support filtering the list with any matching property? Something as below:

    contactList.stream().filter(contact -> contact.anyProperty.equals("dubai").collect(Collectors.toList());
    

    Can someone help me on this, thanks.

    • ernest_k
      ernest_k about 4 years
      Seems like you need full-text search in your database. Have you considered/investigated that?
    • Holger
      Holger about 4 years
      There is no library that can eliminate the fundamental operation necessary for the task. You want all properties checked, you need to check all properties. Any better looking solution only hides the actual work.
  • Mujahid
    Mujahid about 4 years
    Can you please share how to filter JsonObjects, and get the ids for the filtered objects
  • Mujahid
    Mujahid about 4 years
    Thanks, toString() will be the simple trick, i will check the performance.
  • Trần Hoàn
    Trần Hoàn about 4 years
    convert to a json text: String json = new Gson().toJson(yourList); convert to a json array: JsonArray arr = new JsonParser().parse(json).getAsJsonArray(); JsonArray is an Iterable, it's elements are JsonElement, which can convert to JsonObject via .getAsJsonObject() And JsonObject has "entrySet()` Try use them yourself
  • Holger
    Holger about 4 years
    Obviously, concatenating 20+ properties to a string to search it, is more work than just comparing the 20+ properties.
  • Mujahid
    Mujahid about 4 years
    Thanks, will try this.