Generic Comparator Using Class Fields & Lambda Java I have a grid in which i have to display some/all fields of a Java Class,
this grid displays the List of elements of that class type.

I have to sort this list provided:


Class type of the List item
Field name of the class to be sorted


to get field from a class by field name

private static &lt;T&gt; Field getField(Class&lt;T&gt; clazz,String fieldName){
    Field field = null;
    try {
        field = clazz.getDeclaredField(fieldName);
    }catch(java.lang.NoSuchFieldException e){
        e.printStackTrace();
    }
    return field;
}


and the comparator method using lambda, providing class type, field name to sort, and sort order

public static &lt;T&gt; Comparator&lt;T&gt; getGenericComparatorLambda(Class&lt;T&gt; clazz,
        String fieldName,boolean orderByAsc){

    //get field from class
    final Field field = getField(clazz, fieldName);
    field.setAccessible(true);

    //Lambda comparator
    Comparator&lt;T&gt; orderByComparator = (f1, f2) -&gt; {

        Comparable compA = null;
        Comparable compB = null;

        // getting fields from object f1,f2
        try {
            compA =(Comparable) field.get(f1);
            compB =(Comparable) field.get(f2);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        // handling null comparators
        if(compA == null)
            return -1;
        else if(compB == null)
            return 1;

        return compA.compareTo(compB);
    };

    //reverse comparator if order is descending
    if (orderByComparator != null &amp;&amp; !orderByAsc)
        orderByComparator = orderByComparator.reversed();

    return orderByComparator;   
}


As the code consists of Lambda as well as generics of java,
I want an overall analysis of the code, improvements where needed, how much is the performance penalty when using generics with long lists.
If you want suggestions for improvement, here is one:

The following code:

private static &lt;T&gt; Field getField(Class&lt;T&gt; clazz,String fieldName){
    Field field = null;
    try {
        field = clazz.getDeclaredField(fieldName);
    }catch(java.lang.NoSuchFieldException e){
        e.printStackTrace();
    }
    return field;
}


is best rewritten as follows:

private static &lt;T&gt; Field getField(Class&lt;T&gt; clazz,String fieldName) {
    try {
        return clazz.getDeclaredField(fieldName);
    }catch(java.lang.NoSuchFieldException e){
        throw new RuntimeException(e);
    }
}


The following code:

//Lambda comparator
Comparator&lt;T&gt; orderByComparator = (f1, f2) -&gt; {

    Comparable compA = null;
    Comparable compB = null;

    // getting fields from object f1,f2
    try {
        compA =(Comparable) field.get(f1);
        compB =(Comparable) field.get(f2);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    // handling null comparators
    if(compA == null)
        return -1;
    else if(compB == null)
        return 1;

    return compA.compareTo(compB);
};


is best rewritten as follows:

Comparator&lt;T&gt; orderByComparator = (f1, f2) -&gt; {
    Comparable compA, compB;
    try {
        compA = (Comparable) field.get(f1);
        compB = (Comparable) field.get(f2);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
    return Objects.compare(compA, compB, Comparator.naturalOrder());
};


in short:


lose the useless comments; a comment is only useful if it says something that is not immediately obvious by looking at the code
do not initialize variables to nonsensical values, because then the compiler will not warn you when you forget to assign something meaningful to them
if you do not know what to do with an exception, do not print a stack trace, throw a runtime exception instead.


Generics and lambdas have no impact on performance.