【发布时间】:2023-03-19 07:35:01
【问题描述】:
您好,我有一个型号为TopStory 的安卓应用程序。我想创建一个TopStory(topStories)的集合,它按值(即时间)对项目进行排序,每当添加新项目时。新添加的项目将位于正确的索引处(按值时间排序)(即:创建一个已经排序的集合,以便每当我们添加新项目时,它都会自动插入到正确的位置)
这是我的模型
public class TopStory {
private int id;
private String title;
private String author;
private int score;
private JSONArray kids;
private long time;
private String url;
public TopStory() {
}
public TopStory(int id, String title, String author, int point, long time,String url) {
this.id = id;
this.title = title;
this.author = author;
this.score = point;
this.time = time;
this.url = url;
}
我应该使用什么? PriorityQueue, TreeMap,...?如何创建那种类型的集合?非常感谢任何帮助。谢谢。
【问题讨论】:
-
见
java.util.Collections#binarySearch -
你好 binarySearch 是用来搜索值以找到它的索引吗?但我们想创建一个已经排序的集合?
-
它返回:
the non-negative index of the element, or a negative index which is the -index - 1 where the element would be inserted
标签: android sorting collections priority-queue