vendredi 24 mars 2017

Pass generic type class method as parameter

Is there any way to pass a method of a generic type as a parameter? What I try to achieve is something like this:

ListUtils.mapObjectToField( entries, Contact.getZipCode );

I pass a List<T> and a method from the generic class T. So far I am fine with the generic List but I am not able to achieve the method call.

public static <T, U> List<U> mapObjectToField( List<T>elements, /*generic method*/ )
{
    List<U> result = new ArrayList<>();

    for ( T current : elements )
    {
        result.add(current./*generic method*/);
    }
    return result;
}

No Java 8 as I am coding for Android min SDK 14.





Aucun commentaire:

Enregistrer un commentaire