Treeset根據值進行大小的排序
阿新 • • 發佈:2019-02-10
package com.hh.racejob.oneoff.levelrank; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.TreeSet; import com.hh.hhserver.extensions.db.DbManager; import com.hh.hhserver.extensions.db.ResultObjectBuilder; import com.hh.job.CustomStarter; import com.hh.job.JobBase; public class GetLevelRank extends JobBase { private final static int DB_PART = 1000 * 10000; private final static int DEFAULT_TABLE_PART = 100 * 10000; private int partUnit = DEFAULT_TABLE_PART * 10; private static String SELECT_USER_INFO ="SELECT UserId, Nickname,Level" + " FROM UserInfo{0} order by Level desc limit 100"; private static String INSERT="INSERT INTO RankingData VALUES(?,?,?,NOW())"; private static String DELETE="DELETE FROM RankingData WHERE NAME='level'"; private final static int TOPCOUNT=100; private TreeSet<Content> set = new TreeSet<Content>(); public static void main(String[]args) { CustomStarter.main(new String[]{"GetLevelRank", "com.hh.racejob.oneoff.levelrank.GetLevelRank", null, null}); } @Override public void doJob(String[] arg0) { //刪除以前的廢棄資料 DbManager.getWorkDb().executeCommand(DELETE); //增加新的資料 for (int dbIndex = 0; dbIndex < DbManager.getPartDbCount(); dbIndex++) { int tableCount = DB_PART / partUnit; for (int i = 0; i < tableCount; i++) { int tableIndex = dbIndex * tableCount + i; String newSQL = SELECT_USER_INFO.replace("{0}", String.valueOf(tableIndex)); newSQL = newSQL.replace("{1}", String.valueOf(tableIndex * partUnit)); newSQL = newSQL.replace("{2}", String.valueOf(tableIndex * partUnit + partUnit)); List<Content> contentList = DbManager.getPartitionDb(dbIndex).executeQuery_ObjectListEx(newSQL, Content.builder); for(Content content : contentList){ if(content!=null) { content.setTime(String.valueOf(System.currentTimeMillis())); set.add(content); } } } } //得到要插入的100條RankData資料 ArrayList<Object[]>rankDatas=new ArrayList<>(); Iterator<Content> it = set.iterator(); int count=0; while(it.hasNext()&&count<TOPCOUNT){ count++; Content content=(Content) it.next(); RankData rankData=new RankData(); rankData.setContent(content.toString()); rankData.setDataKey(content.getUserId()+""); rankData.setName("level");//固定 rankData.setAddDate(String.valueOf(System.currentTimeMillis())); rankDatas.add(new Object[]{rankData.getName(),rankData.getDataKey(),rankData.getContent()}); } //批量插入資料庫 DbManager.getWorkDb().executeBatchCommandEx(INSERT, rankDatas); } @SuppressWarnings("rawtypes") static class Content implements Comparable { public int userId; public String nickName; public int level; public String time; public Content() { } public static ResultObjectBuilder<Content>builder=new ResultObjectBuilder<Content>() { @Override public Content build(ResultSet rs) throws SQLException { // TODO Auto-generated method stub Content content=new Content(); content.userId=rs.getInt("UserId"); content.nickName=rs.getString("Nickname"); content.level=rs.getInt("Level"); return content; } }; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } @Override public int compareTo(Object o) { Content content=(Content)o; if(level > content.getLevel()){ return -1; } else if(level == content.getLevel()){ if(userId < content.getUserId()){ return -1; } else{ return 1; } } else { return 1; } } public String toString() { return userId+":"+nickName+":"+level+":"+time; } } class RankData { public String Name; public String DataKey; public String Content; public String AddDate; public RankData() { } public String getName() { return Name; } public void setName(String name) { Name = name; } public String getDataKey() { return DataKey; } public void setDataKey(String dataKey) { DataKey = dataKey; } public String getContent() { return Content; } public void setContent(String content) { Content = content; } public String getAddDate() { return AddDate; } public void setAddDate(String addDate) { AddDate = addDate; } } }
類Content中,當level一樣大的時候,uid小的排序在前面,否則相反;
Compareto中,不要返回0,如果直接返回result可能會出現0的狀況,一般返回值的含義如下:
1.返回 1 那麼當前的值會排在 被比較者 後面。
2.返回 0 那麼當前的值【不會被加入到 TreeSet 中】,因為當前的值【被認為是跟現有的某一個值相等】。
3.返回 -1 會被新增到 被比較者 的前邊。
總結:主要需求:從十張表中,選出level的前 100名;
主要思路是:從十張表中,每張表拿出前100名使用者,放在treeset裡面,然後交給treeset的compare進行比較,就可以得到了