Skip to content

Filtering using predicates

I have used predicates before to filter collections as follows:

package com.byhiras.predicate;

import com.byhiras.domain.Bid;
import com.byhiras.domain.User;

 * List of predicates pertinent to User

public class UserPredicate {

     * Is a bid made by a particular user?
     * @param usr
     * @return
    public static Predicate<Bid> isBidByUser(User usr) {
        return p -> p.getUser().getName().equals(usr.getName());

For the above I used Java 8 and the latest Guava library. However I’m working on an assignment where I am restricted to Java 6 and version 13.0.1 of Guava with zero chance of getting them to update their maven repo.

I am struggling to get a similar type of predicate done in Java 6 as the use of lambda expressions is not allowed.

I would appreciate any help to port this code to Java 6. Thanks



The typical, pre-Java-8 way of doing things looks like

public static Predicate<Bid> isBidByUser(final User usr) {
    return new Predicate<Bid>() {
      @Override public boolean apply(Bid p) {
        return p.getUser().getName().equals(usr.getName());
5 People found this is helpful