D
D
DarkByte20152017-08-10 08:42:44
Java
DarkByte2015, 2017-08-10 08:42:44

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

2 answer(s)
P
protven, 2017-08-10
@DarkByte2015

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

Y
Yuri, 2017-08-10
@YuryBorodkin

there is no such. arrays separately, collections - separately.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question