比较泛型类型Java

时间:2013-04-02 18:10:29

标签: java generics types compare

我在比较泛型类型时遇到问题。在C#i中,我总是这样做:class Element<T, V> where T : IComparable<T>。我的问题是它如何用java编写?

2 个答案:

答案 0 :(得分:12)

我怀疑你想要这样的东西:

class Element<T extends Comparable<T>>

...使用Comparable界面和bounded type parameter

答案 1 :(得分:0)

希望对他人有帮助。

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;
        });
    }
}

用于


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);
    }
}