How do you convert a collection to a collection containing another type? Is that possible with standard Java? For example, you want to convert from Collection<String> to Collection<Integer>. Is that possible?
Now look at this piece of test code, this is how it should work:
@Test
public void testTransformedCollection() {
List<String> strings = Arrays.asList("1", "2", "3");
Collection<Integer> integers = new ConvertCollection<String, Integer>(strings) {
@Override
protected Integer convert(String s) {
return Integer.valueOf(s);
}
};
assertEquals(3, integers.size());
Iterator<Integer> it = integers.iterator();
int[] ints = {1, 2, 3};
for (int i = 0; i < ints.length; i++) {
assertTrue(ints[i] == it.next());
}
}
But does a ConvertCollection exist in any of the frameworks? How would it look like (assuming it would be immutable and only the most important methods are implemented?
Tags: collection, java