A Java class for calculating size of iterable/array Another exercise of Java generics:

import java.util.Collection;

/**
 * Created by IDEA on 16/11/14.
 */
public class Size {
    public static &lt;T, I extends Iterable&lt;T&gt;&gt; int size(I data) {
        final int size;
        if (data instanceof Collection) {
            size = ((Collection&lt;T&gt;) data).size();
        } else {
            int counter = 0;
            for(Object i : data) {
                counter++;
            }
            size = counter;
        }
        return size;
    }

    public static int size(int[] data) {
        return data.length;
    }

    public static int size(double[] data) {
        return data.length;
    }

    public static int size(String[] data) {
        return data.length;
    }
}




import java.util.ArrayList;
import java.util.List;

/**
 * Created by IDEA on 16/11/14.
 */
public class TestSize {
    public static void main(String[] args) {
        int[] x = {1, 2, 3};
        List&lt;Integer&gt; y = new ArrayList&lt;&gt;();
        y.add(1);
        y.add(2);
        System.out.println(Size.size(x));
        System.out.println(Size.size(y));
    }
}
For arrays, you are missing a generic int size(Object[] data) method, which can be written as:

public static &lt;E&gt; int size(E[] data) {
     return data.length;
}


This will apply to String[] as well. You will still need one for each kind of primitive though (boolean, short, int, long, float, double, did I miss anything?)

As for your other method...

public static &lt;T, I extends Iterable&lt;T&gt;&gt; int size(I data) {


This one can be cleaned up significantly.

You don't need the &lt;T&gt; here. In fact, you don't need the &lt;I&gt; either. It doesn't matter what kind of iterable it is so you can use Iterable&lt;?&gt;.

As you're not defining a variable of type I anywhere in your method, nor is using that type in another obscure way, all you're really interested in is that it is an Iterable&lt;T&gt;. So:

public static &lt;T&gt; int size(Iterable&lt;T&gt; data) {


Will have the same effect.

Now, are you using the type &lt;T&gt; ? No. In fact, you are iterating over Object so all you're interested in is that it is Iterable&lt;? extends Object&gt;. Which is the same as Iterable&lt;?&gt; since all classes extends Object.

So now we are left with this:

public static int size(Iterable&lt;?&gt; data) {
    final int size;
    if (data instanceof Collection) {
        size = ((Collection&lt;?&gt;) data).size();
    } else {
        int counter = 0;
        for (Object i : data) {
            counter++;
        }
        size = counter;
    }
    return size;
}


And when introducing early return, this:

public static int size(Iterable&lt;?&gt; data) {
    if (data instanceof Collection) {
        return ((Collection&lt;?&gt;) data).size();
    }
    int counter = 0;
    for (Object i : data) {
        counter++;
    }
    return counter;
}


Now that's a method.



Side note: The only method you have that is actually useful is int size(Iterable&lt;?&gt; data). Otherwise it would be easier to just use data.length or data.size();. However, I understand that this is just an exercise in which case it's fine.