Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 822069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:44:44+00:00 2026-05-15T02:44:44+00:00

I am going through the code which uses Predicate in Java. I have never

  • 0

I am going through the code which uses Predicate in Java. I have never used Predicate. Can someone guide me to any tutorial or conceptual explanation of Predicate and its implementation in Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:44:45+00:00Added an answer on May 15, 2026 at 2:44 am

    I’m assuming you’re talking about com.google.common.base.Predicate<T> from Guava.

    From the API:

    Determines a true or false value for a given input. For example, a RegexPredicate might implement Predicate<String>, and return true for any string that matches its given regular expression.

    This is essentially an OOP abstraction for a boolean test.

    For example, you may have a helper method like this:

    static boolean isEven(int num) {
       return (num % 2) == 0; // simple
    }
    

    Now, given a List<Integer>, you can process only the even numbers like this:

        List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        for (int number : numbers) {
            if (isEven(number)) {
                process(number);
            }
        }
    

    With Predicate, the if test is abstracted out as a type. This allows it to interoperate with the rest of the API, such as Iterables, which have many utility methods that takes Predicate.

    Thus, you can now write something like this:

        Predicate<Integer> isEven = new Predicate<Integer>() {
            @Override public boolean apply(Integer number) {
                return (number % 2) == 0;
            }               
        };
        Iterable<Integer> evenNumbers = Iterables.filter(numbers, isEven);
    
        for (int number : evenNumbers) {
            process(number);
        }
    

    Note that now the for-each loop is much simpler without the if test. We’ve reached a higher level of abtraction by defining Iterable<Integer> evenNumbers, by filter-ing using a Predicate.

    API links

    • Iterables.filter
      • Returns the elements that satisfy a predicate.

    On higher-order function

    Predicate allows Iterables.filter to serve as what is called a higher-order function. On its own, this offers many advantages. Take the List<Integer> numbers example above. Suppose we want to test if all numbers are positive. We can write something like this:

    static boolean isAllPositive(Iterable<Integer> numbers) {
        for (Integer number : numbers) {
            if (number < 0) {
                return false;
            }
        }
        return true;
    }
    
    //...
    if (isAllPositive(numbers)) {
        System.out.println("Yep!");
    }
    

    With a Predicate, and interoperating with the rest of the libraries, we can instead write this:

    Predicate<Integer> isPositive = new Predicate<Integer>() {
        @Override public boolean apply(Integer number) {
            return number > 0;
        }       
    };
    
    //...
    if (Iterables.all(numbers, isPositive)) {
        System.out.println("Yep!");
    }
    

    Hopefully you can now see the value in higher abstractions for routines like “filter all elements by the given predicate”, “check if all elements satisfy the given predicate”, etc make for better code.

    Unfortunately Java doesn’t have first-class methods: you can’t pass methods around to Iterables.filter and Iterables.all. You can, of course, pass around objects in Java. Thus, the Predicate type is defined, and you pass objects implementing this interface instead.

    See also

    • Wikipedia/Higher-order function
    • Wikipedia/Filter (higher-order function)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 423k
  • Answers 423k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not directly. You can download image afer program was started,… May 15, 2026 at 11:40 am
  • Editorial Team
    Editorial Team added an answer You can get the query string like this: import flash.external.ExternalInterface;… May 15, 2026 at 11:40 am
  • Editorial Team
    Editorial Team added an answer You could try disabling IntelliTrace in VS 2010. May 15, 2026 at 11:40 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.