zyks
zyks

Reputation: 621

Comparing generic types Java

I have a problem with comparing generic types. In C# I've always done something like: class Element<T, V> where T : IComparable<T>.

My question is how can it be written in java?

Upvotes: 7

Views: 8844

Answers (2)

Prabu R
Prabu R

Reputation: 11

Hope its helps for others.

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Comparator {
    public static HashMap<Class,Class> primitiveMap = new HashMap<Class,Class>() {{
        put(boolean.class,Boolean.class);
        put(byte.class,Byte.class);
        put(char.class,Character.class);
        put(short.class,Short.class);
        put(int.class,Integer.class);
        put(long.class,Long.class);
        put(float.class,Float.class);
        put(double.class,Double.class);
        put(String.class,String.class);
        put(Boolean.class,Boolean.class);
        put(Byte.class,Byte.class);
        put(Character.class,Character.class);
        put(Short.class,Short.class);
        put(Integer.class,Integer.class);
        put(Long.class,Long.class);
        put(Float.class,Float.class);
        put(Double.class,Double.class);
        put(Number.class, Number.class);
    }};
    
    private static int compare(Class<?> returnType,Object a, Object b) {
        if(returnType == String.class) {
            return ((String)a).compareTo((String)b);
        } else if(returnType == Short.class) {
            return ((Short)a).compareTo((Short)b);
        } else if(returnType == Integer.class) {
            return ((Integer)a).compareTo((Integer)b);
        } else if(returnType == Long.class) {
            return ((Long)a).compareTo((Long)b);
        } else if(returnType == Float.class) {
            return ((Float)a).compareTo((Float)b);
        } else if(returnType == Double.class) {
            return ((Double)a).compareTo((Double)b);
        }
        return 0;
    }
    public static <T> void sortAscending(ArrayList<T> list,Method m) {
        Class<?> returnType = primitiveMap.get(m.getReturnType());
        Collections.sort(list,(a,b) -> {
            try {
                return compare(returnType,m.invoke(a),m.invoke(b));
            } catch(Exception e) {
                System.out.println("Exception occured"+e.getMessage());
            }
            return 0;
        });
    }
    public static <T> void sortDescending(ArrayList<T> list,Method m) {
        Class<?> returnType = primitiveMap.get(m.getReturnType());
        Collections.sort(list,(a,b) -> {
            try {
                return compare(returnType,m.invoke(b),m.invoke(a));
            } catch(Exception e) {
                System.out.println("Exception occured"+e.getMessage());
            }
            return 0;
        });
    }
}

Used in


import java.util.ArrayList;

public class Roommates {
    private String name;
    private int age;
    public Roommates(String name,int age) {
        this.name=name;
        this.age=age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public String toString() {
        return "name="+name+" age="+age;
    }
    
    public static void main(String...strings)throws Exception {
        ArrayList<Roommates> list = new ArrayList<Roommates>();
        list.add(new Roommates("Siva",31));
        list.add(new Roommates("Prabu",25));
        list.add(new Roommates("Suresh",55));
        list.add(new Roommates("Raghav",23));
        list.add(new Roommates("Suganthan",27));
        list.add(new Roommates("Vijay",34));
        
        Comparator.sortDescending(list,Roommates.class.getDeclaredMethod("getName"));
        System.out.println(list);
        
        Comparator.sortAscending(list,Roommates.class.getDeclaredMethod("getAge"));
        System.out.println(list);
    }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500665

I suspect you want something like:

class Element<T extends Comparable<T>>

... using the Comparable interface and a bounded type parameter.

Upvotes: 15

Related Questions