Posts Tagged ‘java’

Neue Foto Gallerie

Wednesday, December 30th, 2009

Moritz Petersen - Photos

Ich habe meine Foto Gallerie mal wieder aktualisiert. Dieses Mal habe ich die Gallerie auf Basis der Google App Engine Platform implementiert. Mehr über meine Erfahrungen bei der Entwicklung der Gallerie kann man hier lesen.

Type conversion collection

Monday, March 16th, 2009

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?