简体   繁体   中英

Guava Collections filter

I am just starting with Guava collections and am trying to write a predicate for a list. I am using Guava 11 since I am on java 5.

Here is my first effort....

public abstract class StatusBean {
    public enum Status {GREEN, AMBER, RED, BLUE};
}

public class RegisterReplicationSynchTime {
    private Status status = null;
    public Status getStatus() {
        return status;
    }
    public void setStatus(Status status) {
        this.status = status;
    }
}

public class RegisterReplicationSynchTime {

    public void doFilter() {

        List<RegisterReplicationSynchTime> registerReplicationSynchTimes = dao.getMyList();

        Predicate<StatusBean.Status> predicate = new Predicate<StatusBean.Status>() {                   
            public boolean apply(StatusBean.Status status) {
                return status != StatusBean.Status.GREEN;                       
            }
        };
        // !!!! COMPILER DOES NOT LIKE THIS LINE!!!!!!
        Collections2.filter(registerReplicationSynchTimes, predicate); 
    }
}

The compiler does not like the call I am making to filter and it is giving me an error as per below.

The method filter (Collection<E>,<Predicate<? super E>) in the type
Collections 2 is not applicable for the arguements
(List<RegisterReplicationSynchTime>,Predicate<StatusBean.Status>)

I am not sure what I need to do to get it right. Can someone please give me a hand?

As the error message tells you, there is a mismatch between the type of elements in your List ( RegisterReplicationSynchTime ) and the elements processed by the predicate ( StatusBean.Status ).

Perhaps you want to change your code to filter a List<StatusBean.Status> ?

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