Android Studio sort()在addAll()方法内部的行上给出了"使用API记录为@since 1.8+"错误.我不太清楚这意味着什么......
我想做的就是一个习惯List,我可以List根据publishedAt属性对其进行排序Articles_Map.
AbstractArticlesList:
public class AbstractArticlesList extends AbstractList<Articles_Map> {
private final List<Articles_Map> l;
public AbstractArticlesList() {
l = new ArrayList<>();
}
public Articles_Map get(int index) {
return l.get(index);
}
public Articles_Map set(int index, Articles_Map element) {
Articles_Map oldValue = l.get(index);
l.add(index, element);
return oldValue;
}
public int size() {
return l.size();
}
private void doAdd(Articles_Map another) {
l.add(another);
}
public void addAll(List<Articles_Map> others) {
for (Articles_Map a : others) {
doAdd(a);
}
l.sort(byPublishedAtComparator);
}
private final Comparator<Articles_Map> byPublishedAtComparator =
new Comparator<Articles_Map>() {
@Override
public int compare(Articles_Map o1, Articles_Map o2) {
if (o1.publishedAt == null) {
return (o2.publishedAt == null) ? 0 : -1;
} else if (o2.publishedAt == null) {
return 1;
}
return o1.publishedAt.compareTo(o2.publishedAt);
}
};
}
Run Code Online (Sandbox Code Playgroud)
文章 _地图:
public class Articles_Map {
public final String title;
public final String description;
public final String url;
public final String urlToImage;
public final Date publishedAt;
public Articles_Map(String title, String description, String url, String urlToImage, Date publishedAt) {
this.title = title;
this.description = description;
this.url = url;
this.urlToImage = urlToImage;
this.publishedAt = publishedAt;
}
}
Run Code Online (Sandbox Code Playgroud)
Com*_*are 11
sort()是API Level 24(Android 7.0)的新手,Android开始采用Java 1.8.
Collections.sort()如果您minSdkVersion的价格低于24,请使用.