问题陈述:我正在尝试使用可比类和collections.sort对复合类进行排序,但出现异常。
class BmetData implements Comparable<BmetData>{ Long mId;
String mName;
String mPath;
String frequency;
List<MValue> mValues;
public int compareTo(BmetData o) {
return mId.compareTo(o.mId);
}}
SubClass-
class MValue implements Comparable<MValue> { Date startTimeInMillis;
Long current;
Long min;
Long max;
Long count;
Long sum;
Long value;
public int compareTo(MValue o) {
return startTimeInMillis.compareTo(o.startTimeInMillis);
}}
我正在从API获取BmetData的响应列表,使用方法-解析api响应字符串-
public static <T> List<T> getJavaListFromJson(ApiResponseHolder apiResponse, Class<T> clazz) {
if (apiResponse.getResponseCode() == 200) {
Gson gson = new Gson();
TypeToken<List<T>> token = new TypeToken<List<T>>() {
};
List<T> pojoList = gson.fromJson(apiResponse.getMessage(), token.getType());
return pojoList;
} else {
return null;
}
}
哪位还给我List<BmetData> list
现在尝试使用获取异常的Collections.sort(list)对其进行排序-
Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
at java.util.Arrays.sort(Arrays.java:1312)
at java.util.Arrays.sort(Arrays.java:1506)
at java.util.ArrayList.sort(ArrayList.java:1462)
at java.util.Collections.sort(Collections.java:141)
at Helper.ApiExecuterUsingToken.main(ApiExecuterUsingToken.java:70)
答案 0 :(得分:0)
而不是使用Comparator进行比较。