Vishal Zanzrukia
Vishal Zanzrukia

Reputation: 4973

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;
    }

Upvotes: 1

Views: 140

Answers (1)

Sezin Karli
Sezin Karli

Reputation: 2525

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

Upvotes: 3

Related Questions