我正在尝试制作一个比较器,可以将任何类型的元素进行比较.我不确定如何创建课程.我只是想让它比较同一类型的两个元素(但客户端给它的任何类型,例如:Integer,String,Double等等),看看哪一个大于另一个.
public class InsertionComparator implements Comparator<T>
{
/**
* Compares two elements.
*
* @param f1 The first element you want to compare.
* @param f2 The second element you want to compare.
* @return -1,0,1 Whether or not one is greater than, less than,
* or equal to one another.
*/
public int compare(<T> element1,<T> element2)
{
if(element1 < element2)
{
return -1;
}
else
{
if(element1 > element2)
{
return 1;
}
else
{
return 0;
}
} …Run Code Online (Sandbox Code Playgroud)