【大数据量获取topn解决方案】
[java]
/**
* list类型有序的存储
* @author wWX154783
*/
public class LimitStorageList
{
private long maxTopN;// topN最大值
private List<KeyCountPair> topNList;// 存储topN的list
public LimitStorageList()
{ }
public LimitStorageList(long maxTopN, List<KeyCountPair> list)
{
this.maxTopN = maxTopN;
this.topNList = list;
}
/**
* 加入keyCountPair对象
*/
void put(KeyCountPair keyCountPair)
{
// 判断topN有没有达到最大值
if (topNList.size() < this.maxTopN)// topN没有超过最大值
{
int index = isExist(keyCountPair);
if (index == -1)// 不重复
{
topNList.add(keyCountPair);
insertSort(topNList);
}
else
{
topNList.remove(keyCountPair);
topNList.add(keyCountPair);
insertSort(topNList);
}
}
else
{
// 判断加入值和最后一个值的大小
if (-1 == (keyCountPair.compareTo(topNList.get(topNList.size() - 1))))// 新加入的值大于topNList最后一个值
{
// 判断新加入对象和topNList里面有没有重复对象
int index = isExist(keyCountPair);
更多精彩教程请关注:电脑系统