patternjavaMinor
A Java class for calculating size of iterable/array
Viewed 0 times
arraysizejavacalculatingforiterableclass
Problem
Another exercise of Java generics:
import java.util.Collection;
/**
* Created by IDEA on 16/11/14.
*/
public class Size {
public static > int size(I data) {
final int size;
if (data instanceof Collection) {
size = ((Collection) 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 y = new ArrayList<>();
y.add(1);
y.add(2);
System.out.println(Size.size(x));
System.out.println(Size.size(y));
}
}Solution
For arrays, you are missing a generic
This will apply to
As for your other method...
This one can be cleaned up significantly.
You don't need the `
int size(Object[] data) method, which can be written as:public static 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 > int size(I data) {This one can be cleaned up significantly.
You don't need the `
here. In fact, you don't need the either. It doesn't matter what kind of iterable it is so you can use Iterable.
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. So:
public static int size(Iterable data) {
Will have the same effect.
Now, are you using the type ? No. In fact, you are iterating over Object so all you're interested in is that it is Iterable. Which is the same as Iterable since all classes extends Object.
So now we are left with this:
public static int size(Iterable data) {
final int size;
if (data instanceof Collection) {
size = ((Collection) 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 data) {
if (data instanceof Collection) {
return ((Collection) 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 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.Code Snippets
public static <E> int size(E[] data) {
return data.length;
}public static <T, I extends Iterable<T>> int size(I data) {public static <T> int size(Iterable<T> data) {public static int size(Iterable<?> data) {
final int size;
if (data instanceof Collection) {
size = ((Collection<?>) data).size();
} else {
int counter = 0;
for (Object i : data) {
counter++;
}
size = counter;
}
return size;
}public static int size(Iterable<?> data) {
if (data instanceof Collection) {
return ((Collection<?>) data).size();
}
int counter = 0;
for (Object i : data) {
counter++;
}
return counter;
}Context
StackExchange Code Review Q#69974, answer score: 3
Revisions (0)
No revisions yet.