package org.jeecg.modules.develop.api.livePort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
// 创建一个泛型类
class MyObject {
private T data;
public MyObject(T data) {
this.data = data;
}
public T getData() {
return data;
}
}
// 创建一个泛型比较器
class MyComparator<T extends Comparable> implements Comparator<MyObject> {
@Override
public int compare(MyObject obj1, MyObject obj2) {
return obj2.getData().compareTo(obj1.getData());
}
}
class Main {
public static void main(String[] args) {
List<MyObject> numbers = new ArrayList<>();
numbers.add(new MyObject<>(5));
numbers.add(new MyObject<>(2));
numbers.add(new MyObject<>(7));
numbers.add(new MyObject<>(10));
numbers.add(new MyObject<>(3));
// 使用自定义泛型比较器对对象列表进行排序
Collections.sort(numbers, new MyComparator<>());
// 打印排序后的结果
for (MyObject<Integer> obj : numbers) {
System.out.println(obj.getData());
}
}
}