我在比较泛型类型时遇到问题。在 C# 中,我一直在做类似的事情:class Element<T, V> where T : IComparable<T>
.
我的问题是如何用java编写?
希望它对其他人有帮助。
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);
}
}