简体   繁体   中英

How can I restrict my method arguments for only Set and List type of collection, not Map?

I have similar kind of two methods for different types of arguments.
1. For java.util.Set

public static <T> List<? super T> consumeSet(Set<? extends T> collection){
    return null;
}

2. For java.util.List BR>

public static <T> List<? super T> consumeList(List<? extends T> collection){
    return null;
}

I want to implement one common method which consumes only implements of List or Set but not Map . I am trying below mentioned method signature. Is it the right way to do this? If not, can anyone suggest me the right way? Thanks.

public static <T,V extends Set<T> & List<T>> List<? super T> consumeListAndSetButNotMap(V collection){
        return null;
    }

You can use the shared Collection interface. That won't accept Map .

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