I have a method that takes in a Collection of type T as a parameter and returns a Collection of type Integer. In this specific instance, I'm trying to return an ArrayList (am I wrong to do that? I figured since an ArrayList inherits from a Collection, that should be okay).
@Test public void test() {
Collection<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(8);
list.add(7);
list.add(3);
list.add(4);
Comparison comp = new Comparison();
int low = 1;
int high = 5;
ArrayList<Integer> actual = SampleClass.<Integer>range(list, low, high, comp);
ArrayList<Integer> expected = new ArrayList<Integer>();
expected.add(2);
expected.add(3);
expected.add(4);
Assert.assertEquals(expected, actual);
}
What am I doing wrong here?
EDIT:
As requested, here is the discussed method:
public static <T> Collection<T> range(Collection<T> coll, T low, T high,
Comparator<T> comp) {
if (coll == null || comp == null) {
throw new IllegalArgumentException("No Collection or Comparator.");
}
if (coll.size() == 0) {
throw new NoSuchElementException("Collection is empty.");
}
ArrayList<T> al = new ArrayList<T>();
for (T t : coll) {
if (comp.compare(t, low) >= 0 && comp.compare(t, high) <= 0) {
al.add(t);
}
}
return al;
}