Hayi
Hayi

Reputation: 6236

Type accepting any collection

I have collection of Set and List so how can call a function who accept any type of collection ?

Set<X> myset = ...;
myfunction(myset);

List<X> mylist = ...;
myfunction(mylist);

myfunction(T collection){
}

So what would be the type of T ?

Upvotes: 1

Views: 460

Answers (3)

Mureinik
Mureinik

Reputation: 311188

If you aren't using generics, T is simply a Collection:

void myfunction(Collection collection)

If you are, you could use T extends Collection

<T extends Collection> void myfunction(T collection)

Upvotes: 0

Adam Siemion
Adam Siemion

Reputation: 16039

Both Set and List extend Collection.

Upvotes: 2

H&#233;ctor
H&#233;ctor

Reputation: 26044

Collection. List and Set interfaces extends Collection.

Upvotes: 2

Related Questions