[Java] Java对大量无序数据获取前N个的代码 →→→→→进入此内容的聊天室

来自 , 2020-07-27, 写在 Java, 查看 107 次.
URL http://www.code666.cn/view/356dc406
  1. /**
  2.  * list类型有序的存储
  3.  * @author wWX154783
  4.  */
  5. public class LimitStorageList
  6. {
  7.     private long maxTopN;// topN最大值
  8.     private List<KeyCountPair> topNList;// 存储topN的list
  9.  
  10.     public LimitStorageList()
  11.     { }
  12.  
  13.     public LimitStorageList(long maxTopN, List<KeyCountPair> list)
  14.     {
  15.         this.maxTopN = maxTopN;
  16.         this.topNList = list;
  17.     }
  18.  
  19.     /**
  20.      * 加入keyCountPair对象
  21.      */
  22.     void put(KeyCountPair keyCountPair)
  23.     {
  24.         // 判断topN有没有达到最大值
  25.         if (topNList.size() < this.maxTopN)// topN没有超过最大值
  26.         {
  27.  
  28.             int index = isExist(keyCountPair);
  29.             if (index == -1)// 不重复
  30.             {
  31.                 topNList.add(keyCountPair);
  32.                 insertSort(topNList);
  33.             }
  34.             else
  35.             {
  36.                 topNList.remove(keyCountPair);
  37.                 topNList.add(keyCountPair);
  38.                 insertSort(topNList);
  39.             }
  40.         }
  41.         else
  42.         {
  43.             // 判断加入值和最后一个值的大小
  44.             if (-1 == (keyCountPair.compareTo(topNList.get(topNList.size() - 1))))// 新加入的值大于topNList最后一个值
  45.             {
  46.                 // 判断新加入对象和topNList里面有没有重复对象
  47.                 int index = isExist(keyCountPair);
  48.                 if (index == -1)// 没有重复
  49.                 {
  50.                     topNList.set(topNList.size() - 1, keyCountPair);
  51.                     insertSort(topNList);
  52.                 }
  53.                 else
  54.                 {
  55.                     topNList.remove(keyCountPair);
  56.                     topNList.add(keyCountPair);
  57.                     insertSort(topNList);
  58.                 }
  59.             }
  60.         }
  61.  
  62.     }
  63.  
  64.     /**
  65.      * 插入排序,只对最后一个值排序
  66.      */
  67.     private static void insertSort(List<KeyCountPair> topNList)
  68.     {
  69.         // 排序
  70.         int i = topNList.size() - 1;
  71.         KeyCountPair temp = topNList.get(i);
  72.         int j = i - 1;
  73.         for (; j >= 0 && (temp.compareTo(topNList.get(j)) == -1); j--)
  74.         {
  75.             topNList.set(j + 1, topNList.get(j));
  76.  
  77.         }
  78.         topNList.set(j + 1, temp);
  79.     }
  80.  
  81.     /**
  82.      * 判断topNList是否存在新加入的对象
  83.      * @return -1不重复
  84.      */
  85.     int isExist(KeyCountPair keyCountPair)
  86.     {
  87.         KeyCountPair keyCountPairTemp;
  88.         for (int i = 0, length = topNList.size(); i < length; i++)
  89.         {
  90.             keyCountPairTemp = topNList.get(i);
  91.             if (keyCountPairTemp.getKey().equals(keyCountPair.getKey()))
  92.             {
  93.                 return i;
  94.             }
  95.         }
  96.         return -1;
  97.     }
  98. ...set,get...
  99. }
  100. //java/6229

回复 "Java对大量无序数据获取前N个的代码"

这儿你可以回复上面这条便签

captcha