Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

How will you run a filter on a Collection?

Pragya Keshap answered on February 4, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • collection laravel filter
  • filter collection (laravel)
  • How do you filter a Collection using SPEL?

  • How will you run a filter on a Collection?

    0

    We can use CollectionUtils of Apache for this purpose. We will

    have to create a Predicate that will define the condition for our

    filter. Then we can apply this Predicate in filter() method.

    Sample code is:

    In this example we filter any names that are less than 5 characters

    long.

    List namesList = asList( "Red", "Blue", "Green" );

    List shortNamesList = new ArrayList();

    shortNamesList.addAll( namesList );

    CollectionUtils.filter( shortNamesList, new Predicate(){

    public boolean evaluate( Object input ) {

    return ((String) input).length() < 5;

    }

    } );

    We can also use Google Guava library for this.

    In Java 8, we can use Predicate to filter a Collection through

    Stream.

    https://www.baeldung.com/java-collection-filtering

    Popularity 1/10 Helpfulness 1/10 Language java
    Source: Grepper
    Tags: filter java
    Link to this answer
    Share Copy Link
    Contributed on Feb 04 2023
    Pragya Keshap
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.