Answer the question
In order to leave comments, you need to log in
Generic class for collections?
Is there some kind of generic class or interface for any collections in java? I know Iterable and Collection, but for example, arrays cannot be cast to them (which is rather strange in my opinion).
PS Alternatively, you can still use streams, but you also need to call the corresponding method for them, but I wanted to be able to simply pass any collection (including an array) into the function. Something like IEnumerable in C#.
Answer the question
In order to leave comments, you need to log in
The most common one is Object. The general is nowhere. What is the real question? How to write a single function to work with any sets? No way. Only crutches when you check whether the input is Collection or Array and execute the corresponding code.
You can overload the method like this
private int someFunction(Collection<?> collection) {
int result = 0;
/* Do some work*/
return result;
}
private int someFunction(int[] array) {
return someFunction(Arrays.asList(array));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question